Php CodeIgniter-可以钩住表单验证->;在回调函数中设置_规则?
嗨,我在通过回调函数连接验证时遇到了一个问题。 $this->form\u validation->set\u消息正在工作,但$this->form->validation->set\u规则不工作。短暂性脑缺血发作Php CodeIgniter-可以钩住表单验证->;在回调函数中设置_规则?,php,codeigniter,Php,Codeigniter,嗨,我在通过回调函数连接验证时遇到了一个问题。 $this->form\u validation->set\u消息正在工作,但$this->form->validation->set\u规则不工作。短暂性脑缺血发作 <?php if (!defined('BASEPATH')) { exit('No direct script access allowed'); } class Users extends CI_Controller { public function valid
<?php
if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
class Users extends CI_Controller {
public function validate_form() {
$data = array('success' => false, 'messages' => array());
$this->load->library('form_validation');
$this->form_validation->set_rules('Employed', '', 'required|callback_is_employed');
$this->form_validation->set_error_delimiters('<p class="text-danger">','<br />','</p>');
if ($this->form_validation->run()) {
$data['success'] = true;
} else {
foreach ($_POST as $key => $value) {
$data['messages'][$key] = form_error($key);
}
}
echo json_encode($data);
}
public function is_employed() {
if($this->input->post('Employed')) {
$this->form_validation->set_message('is_employed', 'data is checked');
$this->form_validation->set_rules('EmployedNoB', 'EmployedNoB', 'trim|required');
$this->form_validation->set_rules('EmployedCA', 'EmployedCA', 'trim|required');
$this->form_validation->set_rules('EmployedCN', 'EmployedCN', 'trim|required');
return false;
}
}
}
?>
回调函数必须返回布尔值或字符串。现在,当Employee存在时,它返回false,破坏了验证过程,在其他情况下(我不知道,验证库如何解释)不会返回任何结果。嗨,谢谢你的回复。有没有办法在回调中使用set_规则?为什么要在回调中使用?将if
移动到其他设置规则
我希望用户选中该框以验证他们是否雇用了员工。如果选中,则隐藏的div框将与要验证的输入文本一起显示。您好,我已经找到了答案。谢谢你的帮助