Welcome Guest, Not a member yet? Register   Sign In
My Form Errors
#1

[eluser]CoffeeBeanDesign[/eluser]
I've written a function to add a 'warning' class to fields which have failed validation. It is not fully tested and I'd appreciate your input - I was thinking of maybe adding in the ability to limit it to only add the class to the first field with an error but this might need some more hacking.

The first bit just adds in one line to the _parse_form_attributes to call the error check. The second function takes a look at the validation data and if the field failed, changes the class attribute.

Code:
function _parse_form_attributes($attributes, $default)
{
    $attributes = _set_form_error_class($attributes);
...

Code:
function _set_form_error_class($attributes = array())
{
       if ( ! isset ( $attributes['name'] ) ) return $attributes;
    
    $OBJ =& _get_validation_object();
    $field_data = $OBJ->_field_data;
    
    if ( ! isset ( $field_data[$attributes['name']] ) ) return $attributes;
    
    if ( $field_data[$attributes['name']]['error'] != '' ) :
    
        if ( isset($attributes['class'] ) ) :
        
            $attributes['class'] .= ' warning';
            
        else :
        
            $attributes['class'] = 'warning';
            
        endif;
    
    endif;
    
    return $attributes;
}


... what do you think??
#2

[eluser]jbreitweiser[/eluser]
I would just use the form_error() function. I am assuming $attributes is an array of fields on the form.


Code:
foreach($attributes as $field){
    if( form_error($field['name']))  {
        if ( isset($attributes['class'] ) ) {
        
            $attributes['class'] .= ' warning';
            
        } else {
        
            $attributes['class'] = 'warning';
            
        }
    }
}
#3

[eluser]CoffeeBeanDesign[/eluser]
$attributes is the the array of attributes passed to all the form functions ( form_input, form_textarea etc ).
#4

[eluser]jbreitweiser[/eluser]
So you are going to call the _parse_form_attributes($attributes, $default) function for each form field. Then just ignore the foreach in my response.

Code:
function _set_form_error_class($attributes = array())
{
    if ( ! isset ( $attributes['name'] ) ) return $attributes;
    
    if( form_error($field['name']))  {
        if ( isset($attributes['class'] ) ) {
        
            $attributes['class'] .= ' warning';
            
        } else {
        
            $attributes['class'] = 'warning';
            
        }
    }
    
    return $attributes;
}
#5

[eluser]CoffeeBeanDesign[/eluser]
Gotcha!! Good call, much better than calling the object. Cheers for that!!
#6

[eluser]CoffeeBeanDesign[/eluser]
Same thing but with less code :

Code:
function _set_form_error_class($a = array())
{
    if ( ! isset ( $a['name'] ) ) return $a;
    
    if( form_error($a['name'])) $a['class'] = ( isset($a['class']) ) ? $a['class'] . ' warning' : 'warning';
    
    return $a;
}
#7

[eluser]jbreitweiser[/eluser]
Yes less code, but not as readable. I don't think there is any advantage to coding it that way because it will take the same time to execute. But it is totally your preference. Just remember that if you go back to this code in 6 months or a year you want it to be easy to understand so you don't spend a lot of time trying to figure out what you did.




Theme © iAndrew 2016 - Forum software by © MyBB