CodeIgniter Forums
Check if a form field has an error - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Check if a form field has an error (/showthread.php?tid=39950)



Check if a form field has an error - El Forum - 03-25-2011

[eluser]Joseph Wensley[/eluser]
Is there a to check if a form field has an error? I don't care what the error is I just need to know if there is any error for the field.

I extended the form helper with this function but I'm not sure if there are cases where it will fail.
Code:
function field_has_error($field)
    {
        if (FALSE === ($OBJ =& _get_validation_object()))
        {
            return TRUE;
        }
        
        if ( ! isset($OBJ->_field_data[$field]['error']) OR $OBJ->_field_data[$field]['error'] == '')
        {
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }



Check if a form field has an error - El Forum - 06-10-2011

[eluser]hugle[/eluser]
Hey,

I simply do: <?php if (form_error('contact_email')) echo ' error'; ?>
If I want to add error class for example


Check if a form field has an error - El Forum - 06-10-2011

[eluser]Brad K Morse[/eluser]
Check out http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#validationrules

You can set rules for each field, then test to make sure all fields passed validation.

Also check out an example http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#tutorial