Welcome Guest, Not a member yet? Register   Sign In
Highlight fields if error in validation
#1

[eluser]darytas[/eluser]
Hi,

I searched google, the wiki and the forums, but nothing interessted found.

Simple question
If a validation failed, I want to assign a custom class to the field.
I Want too color the border in red or something, so the user can see the error.

Sure, I could check "if error not empty then", but this would kinda suck.

Maybe I got the wrong keywords to seach,
glad if you could help me guys Smile

darytas
#2

[eluser]TheFuzzy0ne[/eluser]
I like to show the error message in the label for the field, and I found that I spend a lot of time writing complex looking lines of code in my view, that ended up looking a lot like this.
Code:
<label &lt;?=(form_error('form_field')) ? 'class="formError" ' : ''?&gt;for="form_field">URL: &lt;?=form_error('form_field')?&gt;</label>

Now I have a helper:
Code:
&lt;?php

/**
*
*
* @param string $label_text The text to display in the label
* @param string $field_name The field name of the form input this label relates to
* (assumes you've set the form input id to the specified field name).
* @return string
*/
function get_label($label_text, $field_name)
{
    $label  = "<label for=\"$field_name\"";
    $label .= (form_error($field_name)) ? ' class="formError"' : '';
    $label .= ">" . $label_text . ":";
    $label .= (form_error($field_name)) ? " " . form_error($field_name) : '';
    $label .= "</label>\n";
    
    return $label;
}

// End of file: MY_Form_helper.php
// Location: ./system/application/helpers/MY_Form_helper.php

and I call it from within the view like this:
Code:
&lt;?php echo get_label('Field Title', 'field_name'); ?&gt;

There may be simpler ways to do this, but this method works for me.

I'm sure you can modify the function to suit your needs.
#3

[eluser]darytas[/eluser]
i can Smile

thanks a lot!
#4

[eluser]xwero[/eluser]
Why not give the container tag a class?
Code:
<p&lt;?php if(form_error('form_field') != '') echo ' class="error"' ?&gt;>
<label for="form_field">&lt;input type="text" name="form_field"&gt;
</p>
&lt;!-- or --&gt;
<tr&lt;?php if(form_error('form_field') != '') echo ' class="error"' ?&gt;>
<td><label for="form_field"></td>
<td>&lt;input type="text" name="form_field"&gt;&lt;/td>
</tr>
In your css file you can do
Code:
.error * {border:1px solid #FF0000}
#5

[eluser]darytas[/eluser]
The container is an interesseting solution.

Thanks!
#6

[eluser]TheFuzzy0ne[/eluser]
You can do whatever works for you, however, this works for me due to my page layout.
#7

[eluser]xwero[/eluser]
The idea behind my solution is to leave the design as much as possible up to css. You don't even need to add a class to the error message tag if you don't want to.
#8

[eluser]TheFuzzy0ne[/eluser]
This is how my design displays without a class specified. That's the default display. The class is only added if there's an error. But again, it's what was more applicable to my layout.




Theme © iAndrew 2016 - Forum software by © MyBB