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

[eluser]Exangelus[/eluser]
Hi all,

I'm currently building an framework within CI to easily maintain all sorts of forms.
For this purpose we're making a library that utilizes the excellent FormGen library from Frank Michel and the Livevalidation js class from Alec hill.

Now I'd like to extend CI's Form_validation class because we like to set custom error
messages for each field. Therefore I tend to extend the _execute function and add some functinality to replace set error messages for custom one's.

Here's the code: (I'll add a simple function to set the custom messages)

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)
    {
        //first have pappy do all the work
        parent::_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];
                }
            }
        }
    }
}

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

We willfully cancel the option for multiple error's for one field for our project's purpose Smile

Now something is going wrong, the form always validates false. I'm not quit sure how the return value of _execute influences this but I have tried most logical options (true, false, empty array, nothing) but it always validates false..

Thanks in advance!
#2

[eluser]InsiteFX[/eluser]
Your missing the Constructor unless you left it out of the code!

InsiteFX
#3

[eluser]Exangelus[/eluser]
Completely forgotten, thanks!!!

Edit: unfortunaly this doesn't solve the problem, still always getting getting failed form validation.. anyone further idea's?
#4

[eluser]InsiteFX[/eluser]
Why don't you just use this method?
Code:
// --------------------------------------------------------------------

    /**
     * Set Error Message
     *
     * Lets users set their own error messages on the fly.  Note:  The key
     * name has to match the  function name that it corresponds to.
     *
     * @access    public
     * @param    string
     * @param    string
     * @return    string
     */
    function set_message($lang, $val = '')
    {
        if ( ! is_array($lang))
        {
            $lang = array($lang => $val);
        }

        $this->_error_messages = array_merge($this->_error_messages, $lang);

        return $this;
    }

InsiteFX
#5

[eluser]Exangelus[/eluser]
Well it let's you set a custom error for a specific rule, not for a specific field and that's the functionality we want.
#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
#7

[eluser]Exangelus[/eluser]
Not entirely true I think, the syntax to call set_message is:

Code:
$this->form_validation->set_message('rule', 'Error Message');

You can only specify a rule, not the field that specific rule would apply to as you explain.
Also relocating the call to parent doesn't work unfortunaly. I tried to copy the contents of the complete
_execute function and put them in place of the call to parent and then it works.

So one way or the other it has something to do with the inherintance of the parent's function.. already tried removing the _ prefix of _execute (everywhere in the lib!) but this doesn't change a thing neither..
#8

[eluser]niranjnn01[/eluser]
Hello,

I too had problem with being able to specify a custom error message to a "rule", but not a "field". I solved it as follows(at least to my needs)

I had two fields country and state in drop down box, and they were mandatory fields.

using this..
$this->form_validation->set_message('is_natural_no_zero', 'The %s field is required');

will give two messages
"The Country field is required"
"The State field is required"

Hope it helps someone.

Regards
Rakesh




Theme © iAndrew 2016 - Forum software by © MyBB