我遇到了一个问题,我不明白为什么codeIgniter忽略了我在"form_validation.php"中的规则设置,我在其中使用了以下代码。
$config = array(
'racedetails' => array(
array(
'field' => 'race_memberno',
'label' => 'MembershipNumber',
'rules' => 'required'
),
array(
'field' => 'race_penalties',
'label' => 'Penalties',
'rules' => 'required'
)
);
然后,我使用调用控制器上的验证集
$this->form_validation->run('racedetails');
然而,运行时它总是声明false,表单运行正常,没有返回错误,还有什么我可能遗漏的吗?
以上验证运行功能在以下范围内运行(Dale请求)
public function index($id){
$this->load->library('form_validation');
if($this->form_validation->run('racedetails')){$validated++;}
if($validated != 1){
$this->process_template_build('entries/entry',$data);
}else {
echo "Validation Passed";
}
}
我也遇到了同样的问题,原因是我正在使用自己的Form_validation类全局处理错误输出。
在那里,我忘记了将规则传递给父构造函数。因此,这就是它与自己的MY_Form_Validation类的工作方式:
class MY_Form_validation extends CI_Form_validation {
public function __construct($rules = array())
{
parent::__construct($rules);
$this->_error_prefix = '<div class="alert alert-danger" role="alert"><span class="title"><i class="icon-remove-sign"></i> ERROR</span>';
$this->_error_suffix = '</div>';
}
}
您没有忘记在运行验证之前实际使用$config
文件吗?
$this->form_validation->set_rules($config);