我为具有多个复选框的表单制定了表单验证规则:
$this->form_validation->set_rules('groupcheck[]', 'groupcheck', 'required');
如果在提交时没有选中任何复选框,我的代码将无法通过验证->运行,因为变量不存在:
if ($this->form_validation->run()):
如果我用var检查我的验证规则,验证永远不会通过,因为没有其他表单验证规则:
if(isset($_POST['groupcheck'])):
$this->form_validation->set_rules('groupcheck[]', 'groupcheck', 'required');
endif;
如何管理复选框验证规则,其中var可能不存在,而它将是唯一的表单变量?
问候你,本。
发布于 2011-08-10 18:43:39
不要在CodeIgniter中使用isset()作为CodeIgniter,提供更好的类来检查您正在检查的POST变量是否存在,例如,尝试使用以下代码而不是您的代码:
if($this->input->post('groupcheck')):
$this->form_validation->set_rules('groupcheck[]', 'groupcheck', 'required');
endif;
有关如何在CodeIgniter中使用POST和GET变量的指南,请查看此处的用户指南:http://codeigniter.com/user_guide/libraries/input.html
发布于 2013-02-09 06:24:39
我也有同样的问题。如果您的复选框未选中,则它将永远不会发布。删除复选框的set_rules,在执行其他表单验证规则后,尝试执行以下操作:
if ($this->form_validation->run() == TRUE){ // form validation passes
$my_checkbox_ticked = ($this->input->post('my_checkbox')) ? yes : no;
发布于 2013-02-20 06:29:57
您可以在$this->form_validation->run()
之后比较validation_errors()
如果为FALSE
,则nothing is validate,因此您可以执行某些操作或显示警告
if ($this->form_validation->run() == FALSE) {
if (validation_errors()) {
echo validation_errors();
} else {
echo 'empty';
}
}
https://stackoverflow.com/questions/7015515
复制相似问题