If condition not working with CodeIgniter validation -
i'm using codeigniter! have 3 radio buttons - choose role. if choose 1 of 3 radio buttons, see different dropdowns. want make validation rules - echo validation errors on these dropdowns radio button have chosen. tried doesn't show me validation errors. first, 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 use this:
if ($this->input->post('role_id[]') < 2){ // validation rules } if ($this->input->post('role_id[]') >4 ){ // validation rules }
it shows me validation errors role_id=1. role_id=5 shows me validation errors role_id=1. me? :) thanks!
you passing string 'value' => '2'
, trying compare if identical ===
integer 2
evaluate false
. cast input specified integer , see have got after. i.e. (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')); } }
in example passed value (and since radio, there 1 passed value sure) looking for. also, if right, need null, false or ever value non passed values. have array of values in action controller or model, , check in foreach loop it:
$possible_values = array(1, 2, 5); foreach ($possible_values $p_v) { if ($this->input->post('role_id') == $p_v) { //do want true 'role_id' } else { //do want false 'role_id' } }
Comments
Post a Comment