Welcome Guest, Not a member yet? Register   Sign In
Extend form_validation to add custom error messages
#6

[eluser]InsiteFX[/eluser]
Each Rule is set to a specific field and each field has a specific Rule!
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* MY_Form_validation is used to add field-custom error messages to CI's Form_validation
*
* @param array contains custom error messages for specific fields. key => field, value => the error message
*/
class MY_Form_validation extends CI_Form_validation
{
    private $custom_error_messages = array();
    
    /**
     * Extend the parent's execute to check whether their are custom error messages stored, remove any old errors from this
     * field and than add the custom error
     *
     * @access    private
     * @param    array
     * @param    array
     * @param    mixed
     * @param    integer
     * @return    mixed
     */    
    public function _execute($row, $rules, $postdata = NULL, $cycles = 0)
    {        
        //loop through all fields
        foreach($this->_field_data as $field => $data)
        {
            //check whether their was an error set
            if(isset($this->_field_data[$field]['error']))
            {
                //and if their is a custom error available, put it in
                if(isset($this->custom_error_messages[$field]))
                {
                    $this->_field_data[$field]['error'] = $this->custom_error_messages[$field];
                    $this->_error_array[$field] = $this->custom_error_messages[$field];
                }
            }
        }
        // You were calling this at the top so it never work!
        // Try it here after you have set everything else up.
        parent::_execute($row, $rules, $postdata = NULL, $cycles = 0);
    }
}

/* End of file MY_Form_validation.php */
/* Location: ./application/libraries/MY_Form_validation.php */

InsiteFX


Messages In This Thread
Extend form_validation to add custom error messages - by El Forum - 03-28-2011, 02:34 AM
Extend form_validation to add custom error messages - by El Forum - 03-28-2011, 06:57 AM
Extend form_validation to add custom error messages - by El Forum - 03-28-2011, 06:59 AM
Extend form_validation to add custom error messages - by El Forum - 03-28-2011, 08:56 AM
Extend form_validation to add custom error messages - by El Forum - 03-28-2011, 12:54 PM
Extend form_validation to add custom error messages - by El Forum - 03-28-2011, 08:02 PM
Extend form_validation to add custom error messages - by El Forum - 03-29-2011, 02:01 AM
Extend form_validation to add custom error messages - by El Forum - 04-17-2011, 05:31 AM



Theme © iAndrew 2016 - Forum software by © MyBB