为多个$this->input->post()编写if语句


Codeigniter if statement for multiple $this->input->post()

我有代码检查用户是否已输入数据到文本字段:

if ( $this->input->post('current_password') || 
     $this->input->post('new_password') || 
     $this->input->post('repeat_password') ) {
   return true;
} else {
   return false;
}

为什么上面的代码返回false,而下面的代码返回true ?

if ( $this->input->post('current_password') ) {
   return true;
} else {
   return false;
}

应该可以:

if ( ($this->input->post('current_password')) || 
     ($this->input->post('new_password')) || 
     ($this->input->post('repeat_password')) )
{
    return true;
} 
else 
{
    return false;
}

我认为您需要在||

之间添加额外的( )

这些类型的字段只能表示一件事。你是在用户更改密码表单中使用它们吗?在这种情况下:

//Below means that the user required to fill in all 3 of the fields. None of them must return false (be left blank)
if ($this->input->post('current_password') && $this->input->post('new_password') && $this->input->post('repeat_password') ) {
  return true;
} else {
  return false;
}

但是,codeigniter支持使用表单验证器检查表单字段的更好方法:

$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('newpass', 'New password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
if ($this->form_validation->run() == FALSE) {
  $this->load->view('myform');
} else {
  $this->load->view('formsuccess');
}