我是 joomla 组件开发(J3、MVC)的新手,我正在尝试创建自定义服务器端表单验证规则。
我将 validate="machinename"
添加到我的表单字段并创建了一个文件 models\rules\machinename.php
defined('_JEXEC') or die('Restricted access');
jimport('joomla.form.formrule');
class JFormRuleMachinename extends JFormRule
{
protected $regex = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/';
}
我在 controllers\field.php
中有一个空 Controller
defined('_JEXEC') or die('Restricted access');
// import Joomla controllerform library
jimport('joomla.application.component.controllerform');
class SampleControllerField extends JControllerForm
{
}
模型位于 models\field.php
defined('_JEXEC') or die('Restricted access');
// import Joomla modelform library
jimport('joomla.application.component.modeladmin');
/**
* HelloWorld Model
*/
class SampleModelField extends JModelAdmin
{
public function getTable($type = 'Field', $prefix = 'SampleTable', $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 2.5
*/
public function getForm($data = array(), $loadData = true)
{
// Get the form.
$form = $this->loadForm('com_sample.field', 'field',
array('control' => 'jform', 'load_data' => $loadData));
if (empty($form))
{
return false;
}
return $form;
}
/**
* Method to get the data that should be injected in the form.
*
* @return mixed The data for the form.
* @since 2.5
*/
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_sample.edit.field.data', array());
if (empty($data))
{
$data = $this->getItem();
}
return $data;
}
}
我的组件名称是com_sample
,一切工作正常(新建、编辑、删除),但后来我将验证规则添加到表单的字段中,现在提交表单时出现错误:
JForm::validateField() rule `machinename` missing.
我最好的猜测是我的命名或文件位置有误,但我不确定并且无法通过谷歌找到任何内容。
所以请帮助我...
最佳答案
自己寻找解决方案,似乎需要将规则文件夹路径添加到表单定义中:
<form addrulepath="/administrator/components/com_sample/models/rules">
这解决了我的问题。
关于php - Joomla 3自定义服务器端表单验证规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24455665/