samedi 25 avril 2015

If condition not working with CodeIgniter validation

I'm using CodeIgniter! I have 3 radio buttons - to choose role. If you choose one of the three radio buttons, you see different dropdowns. I want to make validation rules - to echo validation errors only on these dropdowns for the radio button you have chosen. I tried with this but it doesn't show me any validation errors. First, my radio buttons are:

 <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);

My controller is:

 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
}
}

If I use something like this:

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

It shows me validation errors but for role_id=1. For role_id=5 shows me validation errors that are for role_id=1. Could you help me? :) Thanks!

Aucun commentaire:

Enregistrer un commentaire