Codeigniter:选中复选框时表单验证仍然失败
Codeigniter: Form Validation still fails when checkbox is checked
我想知道为什么我的复选框在已经被勾选后仍然失败。它是条款和条件的复选框
控制器:
// Load the registration page
public function registration_page(){
$this->load->view('includes/main_index');
$this->load->view('recycler/forms/register');
$this->load->view('includes/footer');
}
public function accept_terms_conditions()
{
// Checkbox name is 'agree' which checks if the value of $checked
// is equal to 1
$checked = $this->input->post('agree');
return (int) $checked == 1 ? TRUE : FALSE;
}
$this->form_validation->set_rules('agree', '', 'callback_accept_terms_conditions');
if($this->form_validation->run() == FALSE)
{
// After form submission, it always goes here
$this->registration_page();
}
else{ /* Proceed to data insertion */ }
我用print_r函数打印出它的post值,它返回1,但验证仍然失败。 $this->form_validation->运行() == FALSE 表示如果表单验证有错误,会回到register视图,也就是$this->register()方法调用.
print_r
ticked checkbox
有谁知道为什么总是失败?
编辑:
添加注册视图的加载方法 register() 方法
当复选框被选中而不是 1 时,复选框将“打开”作为值。
public function accept_terms_conditions()
{
// returns true if the checkbox 'agree' has been checked
return ($this->input->post('agree') === 'on');
}
而你的条件是错误的,你是说如果验证失败然后调用方法 register 我认为应该反过来。
尝试
public function accept_terms_conditions() {
$checked = ($this->input->post('agree')) ? TRUE : FALSE;
return $checked;
}
我想知道为什么我的复选框在已经被勾选后仍然失败。它是条款和条件的复选框
控制器:
// Load the registration page
public function registration_page(){
$this->load->view('includes/main_index');
$this->load->view('recycler/forms/register');
$this->load->view('includes/footer');
}
public function accept_terms_conditions()
{
// Checkbox name is 'agree' which checks if the value of $checked
// is equal to 1
$checked = $this->input->post('agree');
return (int) $checked == 1 ? TRUE : FALSE;
}
$this->form_validation->set_rules('agree', '', 'callback_accept_terms_conditions');
if($this->form_validation->run() == FALSE)
{
// After form submission, it always goes here
$this->registration_page();
}
else{ /* Proceed to data insertion */ }
我用print_r函数打印出它的post值,它返回1,但验证仍然失败。 $this->form_validation->运行() == FALSE 表示如果表单验证有错误,会回到register视图,也就是$this->register()方法调用.
print_r
ticked checkbox
有谁知道为什么总是失败?
编辑: 添加注册视图的加载方法 register() 方法
当复选框被选中而不是 1 时,复选框将“打开”作为值。
public function accept_terms_conditions()
{
// returns true if the checkbox 'agree' has been checked
return ($this->input->post('agree') === 'on');
}
而你的条件是错误的,你是说如果验证失败然后调用方法 register 我认为应该反过来。
尝试
public function accept_terms_conditions() {
$checked = ($this->input->post('agree')) ? TRUE : FALSE;
return $checked;
}