multiple form validation codeigniter conflict [duplicate]

偶尔善良 提交于 2020-02-02 00:54:05

问题


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

回答1:


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.




回答2:


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;



回答3:


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



来源:https://stackoverflow.com/questions/12795818/multiple-form-validation-codeigniter-conflict

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!