如果条件不能与CodeIgniter验证一起使用

时间:2015-04-25 16:18:55

标签: codeigniter validation if-statement

我正在使用CodeIgniter!我有3个单选按钮 - 选择角色。如果您选择三个单选按钮之一,您会看到不同的下拉菜单。我想制定验证规则 - 仅针对您选择的单选按钮的这些下拉列表回显验证错误。我试过这个,但它没有向我显示任何验证错误。 首先,我的单选按钮是:



 <input type="radio"   name="role_id[]" onClick='showHide(this, true)' id="radio1"  value="1" />
 

   $data=array(
    'name' => 'role_id[]',
    'value' => '2', 
    'id' => 'radio2',
    'onclick' => 'showHide(this, true)'
    );
  echo form_radio($data);

  $data=array(
    'name' => 'role_id[]',
    'value' => '5',
    'id' => 'radio5',
 'onclick' => 'teachers_show(this, true)'
    );
  echo form_radio($data);
&#13;
&#13;
&#13;

我的控制器是:

&#13;
&#13;
 public function register()
    {
      ​
 if ($this->input->post('role_id[]') === 1){
        $this->form_validation->set_rules('first_name', First name', 'trim|required');
        $this->form_validation->set_rules('last_name', 'Last name', 'trim|required'); 
        $this->form_validation->set_rules('username', 'username', 'trim|required|min_length[6]|max_length[12]|is_unique[users.username]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]'); 
        $this->form_validation->set_rules('password2', 'Confirm password', 'trim|required|matches[password]');  
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]');
        $this->form_validation->set_rules('location', 'Location', 'trim|required');
        $this->form_validation->set_rules('school[]', 'School', 'required');
        $this->form_validation->set_rules('class[]', 'Class', 'required');
        $this->form_validation->set_rules('role_id[]', 'Role', 'required');
        $this->form_validation->set_rules('class_divisions[]', 'Class division', 'required');
        $this->form_validation->set_rules('region', 'Region', 'required');
        $this->form_validation->set_rules('teacher[]', 'teacher', 'required');
        
        }
     ​
 elseif ($this->input->post('role_id[]') === 2){
        $this->form_validation->set_rules('first_name', First name', 'trim|required');
        $this->form_validation->set_rules('last_name', 'Last name', 'trim|required'); 
        $this->form_validation->set_rules('username', 'username', 'trim|required|min_length[6]|max_length[12]|is_unique[users.username]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]'); 
        $this->form_validation->set_rules('password2', 'Confirm password', 'trim|required|matches[password]');  
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]');
        $this->form_validation->set_rules('location', 'Location', 'trim|required');
        $this->form_validation->set_rules('school[]', 'School', 'required');
        $this->form_validation->set_rules('class[]', 'Class', 'required');
        $this->form_validation->set_rules('role_id[]', 'Role', 'required');
        $this->form_validation->set_rules('class_divisions[]', 'Class division', 'required');
        $this->form_validation->set_rules('region', 'Region', 'required');
      
        
        }
         elseif ($this->input->post('role_id[]') === 5 ){
            $this->form_validation->set_rules('all_teachers_show', 'ALL Teachers', 'required');

        }
        if ($this->form_validation->run()==FALSE)
        {
            $this->signup();
        }
        else 
        {  ​
//register
}
}
&#13;
&#13;
&#13;

如果我使用这样的东西:

&#13;
&#13;
  if ($this->input->post('role_id[]') < 2){​
           // validation rules
     }
    
   if ($this->input->post('role_id[]') >4 ){​
           // validation rules
     }
&#13;
&#13;
&#13;

它显示了验证错误,但是对于role_id = 1。对于role_id = 5,我显示了role_id = 1的验证错误。 你可以帮帮我吗? :)谢谢!

1 个答案:

答案 0 :(得分:3)

您正在传递字符串'value' => '2'并尝试将identical ===与评估为2的整数FALSE进行比较。 Cast您对指定整数的输入,看看你得到了什么。即(int)$this->input->post('role_id[]') === 2

public function radiobutton()
{
    echo form_open('test/passingthrough');
    echo '<input type="radio" name="myradio" value="1" ' . set_radio('myradio', '1', TRUE) . ' />';
    echo '<input type="radio" name="myradio" value="2" ' . set_radio('myradio', '2') . ' />';
    echo '<input type="radio" name="myradio" value="5" ' . set_radio('myradio', '5') . ' />';
    echo form_submit('mysubmit', 'Submit Radio button!');
}

public function passingthrough()
{
    $this->form_validation->set_rules('mysubmit', '', 'required');
    $this->form_validation->set_rules('myradio', '', 'required');

    if ($this->form_validation->run() == FALSE) {
        redirect('test/radiobutton', 'refresh');
    } else {
        echo '<pre>', var_dump($this->input->post('myradio'));
    }
}

在这个示例中,传递的值(因为它是无线电,肯定会有一个传递的值)将是您正在寻找的东西。但是,如果我找到了你,你需要NULL,FALSE或者非传递值的值。因此,您将在动作控制器或模型中拥有所有值的数组,并在foreach循环中检查它:

$possible_values = array(1, 2, 5);
foreach ($possible_values as $p_v) {
    if ($this->input->post('role_id') == $p_v) {
        //do what do you want with TRUE 'role_id'
    } else {
        //do what do you want with FALSE 'role_id'
    }
}