Welcome Guest, Not a member yet? Register   Sign In
Form validation: style input fields on error
#1

[eluser]drcosmo[/eluser]
Hey,

I generated a form using the form_helper and i'm using the form validation class to validate the form. This works very well.

Is it possible to style the input-fields that generated a validation error? I'd like to add a 'class=error' attribute to the invalid input-fields. This way i can show a smooth red border on the incorrect input-fields.


Thanks!
#2

[eluser]Cayson[/eluser]
Yes, this is possible. You have to set the error delimiters. This let's you convert the default
Code:
<p></p>
tags to your own tags such as
Code:
<span class='form_error'></span>
. You can also create specific delimiters for each field using the, I believe, form helper. This is all outlined in the user guide:

http://ellislab.com/codeigniter/user-gui...delimiters
#3

[eluser]drcosmo[/eluser]
Thanks, but that's not what i meant. I already show a div with a summary of all the errors. What i want (extra) is something like this:

Code:
&lt;input type="text" name="name" value="" id="name" /&gt;

becomes

Code:
&lt;input type="text" name="name" value="" id="name" class="error" /&gt;

when an error occurs for this field.
#4

[eluser]Cayson[/eluser]
Yeah, that's in the guide. Right below delimiters. You use form_error instead of validate_errors in the view page.

http://ellislab.com/codeigniter/user-gui...dualerrors

Code:
&lt;?php echo form_error('field name', '<div class="error">', '</div>'); ?&gt;
#5

[eluser]drcosmo[/eluser]
Thanks, it works. this is my code:

Code:
$options = array(
    'id' => 'name',
    'name' => 'name',
    'value' => set_value('name')
);
if(form_error('name'))
    $options['class'] = 'error';
                
echo form_input($options);




Theme © iAndrew 2016 - Forum software by © MyBB