Welcome Guest, Not a member yet? Register   Sign In
Custom message for form validation?
#1

[eluser]yxxm[/eluser]
Hi all,

I'm pretty new to CodeIgniter here, so forgive if this question has been answered already. I've looked in the docs for setting a custom error message when a field (form) validation fails. What I see in the documentation (Setting Error Messages) is:

Quote:All of the native error messages are located in the following language file: language/english/form_validation_lang.php

To set your own custom message you can either edit that file, or use the following function:
$this->form_validation->set_message('rule', 'Error Message');

So I went ahead and changed 'required' to my custom rule. Obviously this worked, but it changed it for all of the fields (as expected). Essentially, I want the default string for the required rule, but for one of my fields I would like an error message that I explicitly define. So, for example, here is what I'm trying to accomplish:

Code:
// Name is okay with the default required message (as well as the rest of the [omitted] fields)
$this->form_validation->set_rules('name', 'Name', 'trim|required');

// I would like type to have an explicit message of: 'Type must be A, B, or both A and B'
$this->form_validation->set_rules('type[]', 'Type', 'required');

It seems that logically, set_rules should have an optional fourth parameter that allows you to override the default error message for the rule, but keep the rule (behavior) in tact. The only way I see that this can be accomplished is by creating a custom callback that mimics the behavior of required. Am I missing something?

Thanks in advance!
#2

[eluser]bretticus[/eluser]
I agree with you. However, rules are supposed to be linked closely to an error message. And even though it's easy to change the error message (config file or explicit set_message call,) doing this changes the error message for the required rule across the board. So yes, either build a custom callback or extend the Form_validation class (if you plan on reusing the rule.)

Here is an example of extending (not tested but should work.)

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

/*
* Extension to let me add a few more validation rules.
*
*/

class MY_Form_validation extends CI_Form_validation {

    function __construct()
    {
        parent::CI_Form_validation();
        $this->set_message('require_type', 'Your custom message here');
    }

    function require_type($type)
    {
        return ( //condition ) ? TRUE : FALSE;
    }

}
#3

[eluser]yxxm[/eluser]
Hey bretticus,

Thanks for the response. I appreciate the example you posted. The thing is, I won't be using _just_ the type field. The idea is that for any given field, I may want to specify a custom error message. It just seems silly to me (and that it defies the concept of abstraction) that I would have to write a different validation callback function for each custom message. I was thinking that it would be acceptable to make a rule like max_length[n], where you can specify a parameter (where that parameter would be the error message, in this case), except I couldn't find an example in the documentation of how to go about that.

In any event, I tried the example that you posted with limited success. I created the following class:

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

class MY_Form_validation extends CI_Form_validation {
    function __construct() {
        parent::CI_Form_validation();
        echo 'constructed';
    }
    
    function required_message( $str ) {
        echo 'test' . $str;
        if( is_array( $str ) == false ) {
            return ( trim( $str ) == '' ) ? false : true;
        } else {
            return ( empty( $str ) == false );
        }
    }
}

named it 'MY_Form_validation.php', and put it in my /application/libraries folder. Note that the echo statements are in there for testing purposes. I then proceeded to do:

Code:
$this->load->library('form_validation');
            
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('bio', 'Biography', 'trim|required');

...

$this->form_validation->set_rules('type[]', 'Type', 'required_message');

When running this code, I see the 'constructed' message, but it seems like the required_message function never fires. I read over the docs again, thinking maybe I had to add callback_ to the front when using set_rules, but that didn't work either.

Any help would be appreciated!
#4

[eluser]zyddee[/eluser]
I'm experecing the same problem, any solution plz?
#5

[eluser]WanWizard[/eluser]
The callback_ prefix is only needed if you call a method in your controller, not for methods in the form validation class.

Validation methods are only called when there is something to validate. Maybe type[] evaluates to empty?
#6

[eluser]zyddee[/eluser]
Thx,
I also found the solution in this thread, by passing the $config variable Smile

http://ellislab.com/forums/viewthread/148607/




Theme © iAndrew 2016 - Forum software by © MyBB