Welcome Guest, Not a member yet? Register   Sign In
How to show a single form validation message with when multiple forms?
#1

[eluser]Mateo1041[/eluser]
Hi all,

Here's my dilemma. I have 3-4 forms on some pages and wish to only show validation errors for the relevant form being filled out. Currently, an error message is being shown for all forms using validation_errors(), even when it really only concerns the current one being filled out.

Any advice or a way around this?

Thanks much,
- Matt
#2

[eluser]WanWizard[/eluser]
What is your definition of a 'form'?

If by form you mean a section of html enclosed by <form> </form>, then only one of those can be submitted at the time.
So you need to find some way if identifying the form submitted (p.e. hidden field with a name, parameter in the action URL), and then run only the validation of that form.
#3

[eluser]Ivar89[/eluser]
Can't you make a separate controller function for each form...?When you use the first form tell the controller to only validate those fields, I haven't actually tried it and I am no master with CI myself, but this is how I would do it. Sounds logic to me.

EDIT: and like the erik said: don't use same field names
#4

[eluser]erik.brannstrom[/eluser]
I agree with Ivar89. I've done this myself by sending each form to a different method, but having all of them use the same view. As long as the fields of the different forms have unique names (i.e. you can't have two forms that both have a field called 'name'), it all works smoothly.
#5

[eluser]WanWizard[/eluser]
That's what I wrote: "use the action URL".

If you call a different controller, a different method within the same controller, or the same method and use parameters is entirely up to you.
#6

[eluser]Ivar89[/eluser]
Oh well, I haven't used CI for that long so I didn't really get what you meant so I thought of something else(at least I thought it was) to solve it:p
#7

[eluser]Mateo1041[/eluser]
Sorry, I should clarify. Here's what I mean:

http://www.bluewebstudios.com/temp/Validation.png

I'd like each form to show its own validation errors rather than the correct error for all three. I'm mainly wondering if the Form Validation class has something built in that would facilitate this, or if I'd have to finagle something myself. :-)
#8

[eluser]Mateo1041[/eluser]
While I believe I could extend the Form Validation class, it seemed like more than I'm willing to tackle at this point. So here's what I did:

Created the following library functions:

Code:
// Sets/gets the active form to ensure validation messages only show once
   function SetFormID($formid)
   {
      if( strlen($formid) > 0 )
      {
         $this->CI = &get;_instance();
         $this->CI->session->set_userdata('FormValidationID', $formid);
         return 1;
      }
      else { return -1; }
   }
   function GetFormID()
   {
      $this->CI = &get;_instance();
      return $this->CI->session->userdata('FormValidationID');
   }

Used the following in conjunction with the Form Helper in the form's view:

Code:
$formid = "searchkeyword";
   if( $this->general->GetFormID() == $formid ) { echo validation_errors(); }
   echo form_hidden('formid', $formid);

Lastly, I use this in my controller at the top:

Code:
$this->general->SetFormID($_POST['formid']);

Works like a charm for now at least.
#9

[eluser]Unknown[/eluser]
This is the same issue I had. No simple way to display errors relevant only to the form that was submitted. Errors for any form appear in the feedback areas for all forms. My solution was to use form_error('FIELD') where FIELD is the name of the field in that form.

http://ellislab.com/codeigniter/user-gui...ation.html

Code:
<div class="feedback">
    &lt;?php echo form_error('username'); ?&gt;
    &lt;?php echo form_error('li_password'); ?&gt;
</div>
&lt;?php echo form_open('login/process_login', array('id'=>'login-form')) ?&gt;
    <ul>
        <li><label for="username">{label_username}: </label>&lt;input type="text" name="username" value="&lt;?php echo set_value('username'); ?&gt;" id="username" /&gt;&lt;/li>
        <li><label for="li_password">{label_password}: </label>&lt;input type="password" name="li_password" value="" id="li_password" /&gt;&lt;/li>
        <li>&lt;input type="submit" name="login" value="{label_login}" id="login-button" /&gt;&lt;/li>
    </ul>
&lt;/form&gt;

Each form has its own set of form_error() calls.




Theme © iAndrew 2016 - Forum software by © MyBB