Welcome Guest, Not a member yet? Register   Sign In
Setting validation messages for a field/rule pair
#1

[eluser]Madmartigan1[/eluser]
I know that we can set validation messages for different rules, but what about for a specific field/rule pairing?

Example:
_________________________________________________________
field = name
rules = required|min_length[x]

desired messages
Please tell us your name!
Your name must be longer than x letters!


field = password
rules = required|min_length[x]

desired messages
Did you forget to enter a password?
Please enter a password that is at least x characters.

_________________________________________________________

I can clarify if needed, but I think you get what I'm trying to do.

Is it possible?
#2

[eluser]tomcode[/eluser]
You can write Your own callbacks where You can set Your own messages
#3

[eluser]Madmartigan1[/eluser]
Ok.

What about in a loop, something like this:

Code:
$fields[] = array(
    'field'        =>    'field_one',
    'rules'        =>    'required|min_length[3]'
);
$fields[] = array(
    'field'        =>    'field_two',
    'rules'        =>    'required|valid_email'
);

foreach($fields as $k => $v)
{
    $field_name = $fields[$k]['field'];
    foreach(explode('|', $fields[$k]['rules']) as $rule)
    {
        $lang = lang($field_name.'_'.$rule.'_message';
        //there is no third parameter for set_message() as far as i know
        $this->validation->set_message($rule, $lang, $field_name);
        //wish it would load lang line "field_one_required_message" or something like it

    }
}
#4

[eluser]tomcode[/eluser]
I believe one can have only one error message per type of rule.

I would create a callback for every rule/field combination :

field = name
rules = required|name_min_length

field = password
rules = required|password_min_length

For just a few combinations that's good enough and keeps it simple.
#5

[eluser]Madmartigan1[/eluser]
[quote author="tomcode" date="1274311772"]
I would create a callback for every rule/field combination[/quote]

I get that callbacks would work, but it's terrible.

Imagine actually doing that for just the example provided. You would have to write 4 callback functions and add each one to every rule.

Now imagine doing that for real, with possibly dozens of combinations.

Several language lines however, would not be so bad after running the sample code.

I guess there just isn't anything built in to the validation lib for that yet.
Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB