Welcome Guest, Not a member yet? Register   Sign In
Custom errors in Form Validation (basic)
#1

[eluser]Stu Green[/eluser]
Hey all

Just thought I'd share some code that I've been working on. This is a really simple solution to a problem that I've had with the new Form Validation class. You can set custom error messages with it.

Code below, also in my blog post:

http://www.haloweb.co.uk/blog/2009/03/co...ion-class/

If anyone wants to build on it, feel free.

Thanks



Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* MY_Form_validation Class
*
* Extends Form_Validation library
*
* Allows for custom error messages to be added to the error array
*
* Note that this update should be used with the
* form_validation library introduced in CI 1.7.0
*/
class MY_Form_validation extends CI_Form_validation {

    function My_Form_validation()
    {
        parent::CI_Form_validation();
    }

    // --------------------------------------------------------------------

    /**
     * Set Error
     *
     * @access  public
     * @param   string
     * @return  bool
    */  

    function set_error($error = '')
    {
        if (empty($error))
        {
            return FALSE;
        }
        else
        {
            $CI =& get_instance();

            $CI->form_validation->_error_array['custom_error'] = $error;

            return TRUE;
        }
    }

}

?>
#2

[eluser]pistolPete[/eluser]
Since MY_Form_validation is just a subclass of Form_validation, you could use:
Code:
function set_error($error = '')
{
        if (empty($error))
        {
            return FALSE;
        }
        else
        {
           $this->_error_array['custom_error'] = $error;
           return TRUE;
        }
}




Theme © iAndrew 2016 - Forum software by © MyBB