7

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 } 
}
Community
  • 1
  • 1
Ram Guiao
  • 352
  • 1
  • 7
  • 13
  • 2
    `http://stackoverflow.com/questions/5802729/codeigniter-2-forms-on-one-page-validation-errors-problem` – iLaYa ツ Oct 09 '12 at 08:38
  • 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 } } – Ram Guiao Oct 09 '12 at 08:38

3 Answers3

7

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.

lijinma
  • 2,914
  • 1
  • 23
  • 22
5

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;
GBD
  • 15,847
  • 2
  • 46
  • 50
2

If you have different validation errors for each form you can check the output from validation_errors.

As far as I can see validation_errors only allows you to change the delimiters of errors and nothing else. However, you can try showing individual form errors like so: <?php echo form_error('username'); ?>

Flame
  • 343
  • 2
  • 9