Codeigniter 2 is formed on one page, validation_errors problem

On one of my sites, I have 2 forms on one page, I have a problem with validation_errors(); basically what happens is for one of the forms that I check for errors, and if there are any errors, I'm doing some style to color the shortcuts, as the other form just displays errors using echo validation_errors(); . When I submit a form that does not display errors, just erasing the errors, validation errors are displayed on the form. How can i stop this?

+6
source share
2 answers

Your question is a little difficult to read, but if I understand correctly, you have problems checking two separate forms from the same controller or problems with errors from different forms using validation_errors() , which afaik prints ALL errors:

Before starting the check, check for a hidden field, a field that is unique to the form, or you can check the value of a specific submit button.

 <form> <input type="hidden" name="form1" value="whatever"> <input name="form1_email" /> <input type="submit" value="Submit Form 1" /> </form> 

Then you can use any of these methods to check which form was submitted (this example checks if "form1" was submitted):

 <?php // Choose one: if ($this->input->post('form1')): // check the hidden input if ($this->input->post('form1_email')): // OR check a unique value if ($this->input->post('submit') == 'Submit Form 1'): // OR check the submit button value if ($this->form_validation->run()): // process form else: // Create a variable with errors assigned to form 1 // Make sure to pass this to your view $data['form1_errors'] = validation_errors(); endif; endif; // Do same for form 2 

Then, in your opinion, instead of validation_errors() you should use:

 if (isset($form1_errors)) echo $form1_errors; // Print only form1 errors 

If this does not help, let me know and clarify your question by posting your code.

+23
source

What I did separated both forms. The view will look like

  <?php echo validation_errors(); ?> <?php echo form_open('form1'); ?> <form id="form1" action="some_action"> //Inputs </form> <?php echo form_open('form2'); ?> <form id="form2" action="other_action"> //Inputs </form> 

Now in the controller you can have two different functions for each check:

  //Controller function some_action(){ //validate form and code } function other_action(){ //validate form2 and code } 

Now all verification messages will be displayed in one place, but only messages of each form will be displayed. Hope that helps

0
source

All Articles