CodeIgniter Forums
Access validation errors in a helper - 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: Access validation errors in a helper (/showthread.php?tid=38463)



Access validation errors in a helper - El Forum - 02-08-2011

[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?


Access validation errors in a helper - El Forum - 02-08-2011

[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