Possible Duplicate:
Codeigniter 2 forms on one page, validation_errors problem
I have 2 forms in my page. I need to validate them 1 at a time but I think there is a conflict. Here take a look:
when I submit either of the form, both of them show the same error message
I use validation_errors()
to display the messages. How can I validate the form 1 at a time?
Here is the code
public function update_user_info(){
$this->form_validation->set_rules("firstname","First Name","required");
$this->form_validation->set_rules("lastname","Last Name","required");
$this->form_validation->set_rules("middlename","Middle Name","required");
if($this->form_validation->run()===false){
//wrong
}
else { //correct }
}
I just encountered the issue. My solution is:
1.First set the first submit button name = 'update_info'
2.Secondly set the second submit button name = 'change_password'
3.Last change your update_user_info().
public function update_user_info(){
if (isset ($_POST['update_info'])) {
$this->form_validation->set_rules("firstname","First Name","required");
$this->form_validation->set_rules("lastname","Last Name","required");
$this->form_validation->set_rules("middlename","Middle Name","required");
if($this->form_validation->run()===false){
//wrong
}
else { //correct }
}
else if (isset ($_POST['change_password'])){
form_validation of your change password
}
I think this is the easiest way to fix your issue.
Good luck.
You can take one hidden input for each form
First Form:
<input type="hidden" name="form" value="form1" />
Second Form:
<input type="hidden" name="form" value="form2" />
In your controller, you can set array of rules for each form
$config['form1'] = array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'required'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'required'
),
);
$config['form2'] = array(
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'required'
),
);
Now check which hidden field posted
$form = $this->input->post('form')
Now you can set rules as below
$this->form_validation->set_rules($config[$form]);
if ($this->form_validation->run()):
// process form
else:
$data[$form.'_errors'] = validation_errors();
endif;
Now in your view file
if (isset($form1_errors)) echo $form1_errors;
if (isset($form2_errors)) echo $form2_errors;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With