Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple form validation codeigniter conflict [duplicate]

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:

enter image description here

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 } 
}
like image 876
Ram Guiao Avatar asked Oct 09 '12 08:10

Ram Guiao


2 Answers

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.

like image 74
lijinma Avatar answered Sep 20 '22 10:09

lijinma


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;
like image 20
GBD Avatar answered Sep 22 '22 10:09

GBD