J3.x:تطوير مكون MVC/اضافة التحققات
From Joomla! Documentation
< J3.x:Developing an MVC Component
المقالات في هذه السلسلة
اضافة نمط قائمة الى جزء الموقع
اضافة موديل الى جزء الموقع
اضافة طلب متحول في نوع القائمة
استخدام قاعدة البيانات
واجهة خلفية بسيطة
اضافة ادارة لغة
اضافة أفعال في الواجهة الخلفية
اضافة ديكور الى الواجهة الخلفية
اضافة التحقيقات
اضافة فئات
اضافة اعداد
اضافة ملف سكريبت لتثبيت/فك تثبيت/تحديث
Adding a Frontend Form
استخدام ميزات تصفية اللغة
- Adding a Modal
- Adding Associations
- Adding Checkout
- Adding Ordering
- Adding Levels
- Adding Versioning
- Adding Tags
- Adding Access
- Adding a Batch Process
- Adding Cache
- Adding a Feed
اضافة مخدم تحديث
هذه سلسلة من عدة مقالات من الدروس حول كيفية تطوير موديل-عرض-موجه مكون لنسخة Joomla! .
تبدأ مع مقدمة, وتستعرض المقالات في هذه السلسلة باستخدام زر التنقل في الأسفل أو الصندوق الأيمن ("المقالات في هذه السلسلة").
مقدمة
هذا الدرس هو جزء من درس تطوير مكون MVC لـ Joomla! 3.2 . نشجعك على قراءة الجزء السابق من الدرس قبل قراءة هذا الدرس. شاهد Form validation لمزيد من المعلومات العامة على (قواعد) تخققات النموذج.
You can watch a video associated with this step at Step 11, adding verifications. This and subsequent videos are recorded in HD; click on the YouTube settings wheel to vieiw at best resolution.
التحقق من النموذج (جانب العميل)
See Client-side form validation for further tutorial information relating to client-side verification. يمكن التحقق من النماذج على جانب العميل باستخدام شفرة جافاسكريبت. في الملف admin/views/helloworld/tmpl/edit.php ، ضع هذه الأسطر: admin/views/helloworld/tmpl/edit.php
<?php
/**
* @package Joomla.Administrator
* @subpackage com_helloworld
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access
defined('_JEXEC') or die('Restricted access');
?>
<form action="<?php echo JRoute::_('index.php?option=com_helloworld&layout=edit&id=' . (int) $this->item->id); ?>"
method="post" name="adminForm" id="adminForm" class="form-validate">
<div class="form-horizontal">
<fieldset class="adminform">
<legend><?php echo JText::_('COM_HELLOWORLD_HELLOWORLD_DETAILS'); ?></legend>
<div class="row-fluid">
<div class="span6">
<?php foreach ($this->form->getFieldset() as $field): ?>
<div class="control-group">
<div class="control-label"><?php echo $field->label; ?></div>
<div class="controls"><?php echo $field->input; ?></div>
</div>
<?php endforeach; ?>
</div>
</div>
</fieldset>
</div>
<input type="hidden" name="task" value="helloworld.edit" />
<?php echo JHtml::_('form.token'); ?>
</form>
ربما لاحظت بأن نموذج html يحنوي في الملف admin/views/helloworld/tmpl/edit.php' الآن يملك صنف form-validate css . ونحن أضفنا استدعاء JHtml::_('behavior.formvalidator'); لنخبر جوملا لاستخدام جافاسكريبت من تحقق النموذج.
عدل الملف admin/models/forms/helloworld.xml ليشير الى وجوب التحقق من حقل greeting :
<?xml version="1.0" encoding="utf-8"?>
<form
addrulepath="/administrator/components/com_helloworld/models/rules"
>
<fieldset>
<field
name="id"
type="hidden"
/>
<field
name="greeting"
type="text"
label="COM_HELLOWORLD_HELLOWORLD_GREETING_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_GREETING_DESC"
size="40"
class="inputbox validate-greeting"
validate="greeting"
required="true"
default=""
/>
</fieldset>
</form>
لاحظ حاليا بأن صنف css هو الآن "inputbox validate-greeting" والخاصية required موضوعة على true. وهذا يعني أن الحقل مطلوب ويجب التحقق منه بواسطة handler من منصة التحقق من النموذج في جوملا.
باستخدام مدير الملف المفضل لديك والمحرر ضع ملف <admin/models/forms/helloworld.js يحتوي :
admin/models/forms/helloworld.js
jQuery(function() {
document.formvalidator.setHandler('greeting',
function (value) {
regex=/^[^0-9]+$/;
return regex.test(value);
});
});
وتضيف معالج لمدقق النموذج لجوملا للحقول التي تملك صنف css "validate-greeting" .في كل مرة يتم تعديل الحقل greeting، وسيتم تنفيذ معالج للتحقق من صحتها (أي أرقام). الخطوة الأخيرة هي التحقق من النموذج عند الضغط على زر حفظ. باستعمال مدير الملف المفضل والمحرر أنشئ ملف admin/views/helloworld/submitbutton.js يحتوي
Joomla.submitbutton = function(task)
{
if (task == '')
{
return false;
}
else
{
var isValid=true;
var action = task.split('.');
if (action[1] != 'cancel' && action[1] != 'close')
{
var forms = jQuery('form.form-validate');
for (var i = 0; i < forms.length; i++)
{
if (!document.formvalidator.isValid(forms[i]))
{
isValid = false;
break;
}
}
}
if (isValid)
{
Joomla.submitform(task);
return true;
}
else
{
alert(Joomla.JText._('COM_HELLOWORLD_HELLOWORLD_ERROR_UNACCEPTABLE',
'Some values are unacceptable'));
return false;
}
}
}
وهذا التابع ستيحقق من أن كافة النماذج التي تملك صنف "form-validate" css تم التحقق منه. لاحظ أنه سيظهر رسالة تحذير مترجمة من منصة جوملا.
إن صنف عرض HelloWorldViewHelloWorld يجب تعديله ليستخدم ملفات جافا سكريبت التالية:
admin/views/helloworld/view.html.php
<?php
/**
* @package Joomla.Administrator
* @subpackage com_helloworld
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
/**
* HelloWorld View
*
* @since 0.0.1
*/
class HelloWorldViewHelloWorld extends JViewLegacy
{
/**
* View form
*
* @var form
*/
protected $form = null;
/**
* Display the Hello World view
*
* @param string $tpl The name of the template file to parse; automatically searches through the template paths.
*
* @return void
*/
public function display($tpl = null)
{
// Get the Data
$this->form = $this->get('Form');
$this->item = $this->get('Item');
$this->script = $this->get('Script');
// Check for errors.
if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode('<br />', $errors));
return false;
}
// Set the toolbar
$this->addToolBar();
// Display the template
parent::display($tpl);
// Set the document
$this->setDocument();
}
/**
* Add the page title and toolbar.
*
* @return void
*
* @since 1.6
*/
protected function addToolBar()
{
$input = JFactory::getApplication()->input;
// Hide Joomla Administrator Main menu
$input->set('hidemainmenu', true);
$isNew = ($this->item->id == 0);
if ($isNew)
{
$title = JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLD_NEW');
}
else
{
$title = JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLD_EDIT');
}
JToolbarHelper::title($title, 'helloworld');
JToolbarHelper::save('helloworld.save');
JToolbarHelper::cancel(
'helloworld.cancel',
$isNew ? 'JTOOLBAR_CANCEL' : 'JTOOLBAR_CLOSE'
);
}
/**
* Method to set up the document properties
*
* @return void
*/
protected function setDocument()
{
JHtml::_('behavior.framework');
JHtml::_('behavior.formvalidator');
$isNew = ($this->item->id < 1);
$document = JFactory::getDocument();
$document->setTitle($isNew ? JText::_('COM_HELLOWORLD_HELLOWORLD_CREATING') :
JText::_('COM_HELLOWORLD_HELLOWORLD_EDITING'));
$document->addScript(JURI::root() . $this->script);
$document->addScript(JURI::root() . "/administrator/components/com_helloworld"
. "/views/helloworld/submitbutton.js");
JText::script('COM_HELLOWORLD_HELLOWORLD_ERROR_UNACCEPTABLE');
}
}
هذا العرض الآن:
- تحقق من أن الموديل لايوجد به أخطاء،
- أضاف ملفين جافا سكريبت،
- حقن ترجمة جافاسكريبت باستخدام تابع جوملا JText::script.
You will also notice our two lines adding additional behaviors when injecting our scripts. By default, Joomla injects the Javascript we specify into the page before its own global Javascript libraries, such as jQuery and Joomla Core. As our Javascript code directly utilises the Joomla object, loading it onto the page before the global Javascript libraries will result in immediate failure, and the form validation will not run.
By specifying these two behaviors via the JHtml helper class prior to injecting our own Javascript, we ensure they are loaded first on the page, and our code will execute correctly. You would normally see JHtml used inside a template, but as our Javascript is added for each child template, it's better to specify the external dependency here.
الخطوة الأخيرة هي تزويد التابع getScript في الموديل HelloWorldModelHelloWorld:
admin/models/helloworld.php
<?php
/**
* @package Joomla.Administrator
* @subpackage com_helloworld
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
/**
* HelloWorld Model
*
* @since 0.0.1
*/
class HelloWorldModelHelloWorld extends JModelAdmin
{
/**
* Method to get a table object, load it if necessary.
*
* @param string $type The table name. Optional.
* @param string $prefix The class prefix. Optional.
* @param array $config Configuration array for model. Optional.
*
* @return JTable A JTable object
*
* @since 1.6
*/
public function getTable($type = 'HelloWorld', $prefix = 'HelloWorldTable', $config = array())
{
return JTable::getInstance($type, $prefix, $config);
}
/**
* Method to get the record form.
*
* @param array $data Data for the form.
* @param boolean $loadData True if the form is to load its own data (default case), false if not.
*
* @return mixed A JForm object on success, false on failure
*
* @since 1.6
*/
public function getForm($data = array(), $loadData = true)
{
// Get the form.
$form = $this->loadForm(
'com_helloworld.helloworld',
'helloworld',
array(
'control' => 'jform',
'load_data' => $loadData
)
);
if (empty($form))
{
return false;
}
return $form;
}
/**
* Method to get the script that have to be included on the form
*
* @return string Script files
*/
public function getScript()
{
return 'administrator/components/com_helloworld/models/forms/helloworld.js';
}
/**
* Method to get the data that should be injected in the form.
*
* @return mixed The data for the form.
*
* @since 1.6
*/
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState(
'com_helloworld.edit.helloworld.data',
array()
);
if (empty($data))
{
$data = $this->getItem();
}
return $data;
}
}
التحقق من النموذج (جانب المخدم)
See Server-side form validation for further tutorial information relating to server-side verification. التحقق من النموذج على جانب المخدم يتم عن طريق مورث JControllerForm . الذي حددناه في ملف admin/models/forms/helloworld.xml والذي سيستخدم تابع التحقق في المخدم الملف greeting.php
باستخدام مدير الملف المفضل لديك والمحرر ضع ملف admin/models/rules/greeting.php يحتوي :
admin/models/rules/greeting.php
<?php
/**
* @package Joomla.Administrator
* @subpackage com_helloworld
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
/**
* Form Rule class for the Joomla Framework.
*/
class JFormRuleGreeting extends JFormRule
{
/**
* The regular expression.
*
* @access protected
* @var string
* @since 2.5
*/
protected $regex = '^[^0-9]+$';
}
لاحظ أنه لايوجد تابع هنا - وهذا موروث من JFormRule (الموجود في libraries/joomla/form /rule.php). وكل ماهو مطلوب هو سلسلة regex لاختبارها.
حزم المكون
محتوياات فهرس الشفرة الخاص بك
- helloworld.xml
- site/helloworld.php
- site/index.html
- site/controller.php
- site/views/index.html
- site/views/helloworld/index.html
- site/views/helloworld/view.html.php
- site/views/helloworld/tmpl/index.html
- site/views/helloworld/tmpl/default.xml
- site/views/helloworld/tmpl/default.php
- site/models/index.html
- site/models/helloworld.php
- site/language/index.html
- site/language/en-GB/index.html
- site/language/en-GB/en-GB.com_helloworld.ini
- admin/index.html
- admin/helloworld.php
- admin/controller.php
- admin/sql/index.html
- admin/sql/install.mysql.utf8.sql
- admin/sql/uninstall.mysql.utf8.sql
- admin/sql/updates/index.html
- admin/sql/updates/mysql/index.html
- admin/sql/updates/mysql/0.0.1.sql
- admin/sql/updates/mysql/0.0.6.sql
- admin/models/index.html
- admin/models/fields/index.html
- admin/models/fields/helloworld.php
- admin/models/helloworlds.php
- admin/models/helloworld.php
- admin/models/forms/filter_helloworlds.xml
- admin/models/forms/index.html
- admin/models/forms/helloworld.js
- admin/models/forms/helloworld.xml
- admin/models/rules/greeting.php
- admin/models/rules/index.html
- admin/controllers/helloworld.php
- admin/controllers/helloworlds.php
- admin/controllers/index.html
- admin/views/index.html
- admin/views/helloworld/index.html
- admin/views/helloworld/view.html.php
- admin/views/helloworld/tmpl/index.html
- admin/views/helloworld/tmpl/edit.php
- admin/views/helloworld/submitbutton.js
- admin/views/helloworlds/index.html
- admin/views/helloworlds/view.html.php
- admin/views/helloworlds/tmpl/index.html
- admin/views/helloworlds/tmpl/default.php
- admin/tables/index.html
- admin/tables/helloworld.php
- admin/language/index.html
- admin/language/en-GB/index.html
- admin/language/en-GB/en-GB.com_helloworld.ini
- admin/language/en-GB/en-GB.com_helloworld.sys.ini
- media/index.html
- media/images/index.html
- media/images/tux-16x16.png
- media/images/tux-48x48.png
انشئ ملف مضغوط عن هذا الفهرس أو حمل مباشرة من archive وثبته باستخدام مدير الملحقات في Joomla!. بامكانك اضافة عنصر قائمة لهذا المكون باستخدام مدير القائمة في الخلفية.
helloworld.xml
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.0" method="upgrade">
<name>COM_HELLOWORLD</name>
<!-- The following elements are optional and free of formatting constraints -->
<creationDate>January 2018</creationDate>
<author>John Doe</author>
<authorEmail>john.doe@example.org</authorEmail>
<authorUrl>http://www.example.org</authorUrl>
<copyright>Copyright Info</copyright>
<license>License Info</license>
<!-- The version string is recorded in the components table -->
<version>0.0.11</version>
<!-- The description is optional and defaults to the name -->
<description>COM_HELLOWORLD_DESCRIPTION</description>
<install> <!-- Runs on install -->
<sql>
<file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
</sql>
</install>
<uninstall> <!-- Runs on uninstall -->
<sql>
<file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>
</sql>
</uninstall>
<update> <!-- Runs on update; New since J2.5 -->
<schemas>
<schemapath type="mysql">sql/updates/mysql</schemapath>
</schemas>
</update>
<!-- Site Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /site/ in the package -->
<files folder="site">
<filename>index.html</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<folder>views</folder>
<folder>models</folder>
</files>
<languages folder="site/language">
<language tag="en-GB">en-GB/en-GB.com_helloworld.ini</language>
</languages>
<media destination="com_helloworld" folder="media">
<filename>index.html</filename>
<folder>images</folder>
</media>
<administration>
<!-- Administration Menu Section -->
<menu link='index.php?option=com_helloworld' img="../media/com_helloworld/images/tux-16x16.png">COM_HELLOWORLD_MENU</menu>
<!-- Administration Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /admin/ in the package -->
<files folder="admin">
<!-- Admin Main File Copy Section -->
<filename>index.html</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<!-- SQL files section -->
<folder>sql</folder>
<!-- tables files section -->
<folder>tables</folder>
<!-- models files section -->
<folder>models</folder>
<!-- views files section -->
<folder>views</folder>
<!-- controllers files section -->
<folder>controllers</folder>
</files>
<languages folder="admin/language">
<language tag="en-GB">en-GB/en-GB.com_helloworld.ini</language>
<language tag="en-GB">en-GB/en-GB.com_helloworld.sys.ini</language>
</languages>
</administration>
</extension>