CodeIgniter Forums

Full Version: Access validation errors in a helper
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]richzilla[/eluser]
Is there any way to access form validation errors in somewhere other than a view? I want to access any potential form validation errors in a helper?

El Forum

[eluser]Twisted1919[/eluser]
Code:
if ( ! function_exists('_get_validation_object'))
{
    function &_get_validation_object()
    {
        $CI =& get_instance();

        // We set this as a variable since we're returning by reference
        $return = FALSE;

        if ( ! isset($CI->load->_ci_classes) OR  ! isset($CI->load->_ci_classes['form_validation']))
        {
            return $return;
        }

        $object = $CI->load->_ci_classes['form_validation'];

        if ( ! isset($CI->$object) OR ! is_object($CI->$object))
        {
            return $return;
        }

        return $CI->$object;
    }
}

And the access to the validation object in your functions:


function form_error($field = '', $prefix = '', $suffix = '')
    {
        if (FALSE === ($OBJ =& _get_validation_object()))
        {
            return '';
        }

        return $OBJ->error($field, $prefix, $suffix);
    }


Maybe you can take a look at the form_helper.php, it has it all Wink