CodeIgniter Forums
Form Validation Errors - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Form Validation Errors (/showthread.php?tid=31317)



Form Validation Errors - El Forum - 06-14-2010

[eluser]_steve[/eluser]
I've just started using CI and already I'm loving the validation class and the form helper.
I am aware you can show individual error messages in custom delimiters:
Code:
&lt;?php echo form_error('field name', '<div class="error">', '</div>'); ?&gt;

However, after searching Google and the forum, I can't find anything on styling the individual fields which failed validation.
Any suggestions?


Form Validation Errors - El Forum - 06-14-2010

[eluser]daelsepara[/eluser]
... more of a CSS issue...

perhaps, you can have multiple "error" classes in you style sheets.

e.g.

Code:
&lt;?php echo form_error('field1', '<div class="error1">', '</div>'); ?&gt;
&lt;?php echo form_error('field2', '<div class="error2">', '</div>'); ?&gt;
&lt;?php echo form_error('field3', '<div class="error3">', '</div>'); ?&gt;



Form Validation Errors - El Forum - 06-15-2010

[eluser]Simian Studios[/eluser]
[quote author="_steve" date="1276574179"]styling the individual fields which failed validation.
Any suggestions?[/quote]

So, you want to apply a class to the actual form input itself, if I understand correctly.

Not sure if this is the "correct" way to do it, but it will work..

Code:
&lt;input type="text" name="name" id="name"&lt;?php if(form_error('name') !== ''){ ?&gt; class="error" &lt;?php } ?&gt;value="&lt;?php echo set_value('name'); ?&gt;" /&gt;

The trick is that form_error() will return a string if there is an error, or a null value if not, so we can test against that and set the class accordingly.


Form Validation Errors - El Forum - 06-15-2010

[eluser]_steve[/eluser]
Sorry daelsepara, my question was a little unclear, but Simian Studios has what I was looking for.
From what I could see from the user guide, there is no "correct" way, so this will do just fine.

Thanks both.


Form Validation Errors - El Forum - 06-16-2010

[eluser]Simian Studios[/eluser]
You're welcome Smile