Welcome Guest, Not a member yet? Register   Sign In
Confused with set_message validation function
#1

[eluser]CARP[/eluser]
Hi guys
I've been trying to replace default error messages when validating my forms, but I cannot get it to work.
After searching in forums, I see that the set_message function is called when using callback functions (custom validation routines) but I won't create 1 custom callback routine for just displaying my custom error message

This is my code

Code:
$rules['usernamefield']     = 'trim|required';
    $rules['emailfield']         = 'trim|required';

    $this->validation->set_message($rules['usernamefield'], 'You must fill this field!');
    $this->validation->set_message('usernamefield', 'You must fill this field!');

    $this->validation->set_rules($rules);
    $this->validation->set_error_delimiters('<span class="error">', '</span><br />');

    $fields['usernamefield']    = 'Your Username';
    $fields['emailfield']    = 'Your Email address';

    $this->validation->set_fields($fields);

but this line
Code:
$this->validation->set_message($rules['usernamefield'], 'You must fill this field!');
nor this one
Code:
$this->validation->set_message('usernamefield', 'You must fill this field!');
aren't working

Should I use set_message only when using callback custom validation functions?

Thanks a lot in adv.
#2

[eluser]charlie spider[/eluser]
if you write:

Code:
$rules['usernamefield']     = 'trim|required';
$rules['emailfield']         = 'trim|required';

then 'usernamefield' refers to a field name
and 'required' refers to a rule.

Code:
$this->validation->set_message('rule', 'error message');
allows you to write custom error messages for a specific rule, not a specific field name.

so you would write:

Code:
$this->validation->set_message('required', 'You must fill this field!');

not:

Code:
$this->validation->set_message('usernamefield', 'You must fill this field!');

but then any form input with the 'required' rule applied to it would receive the 'You must fill this field!' error message if it was left out upon the form being submited.

you can also use:

Code:
$this->validation->set_message('rule', 'error message');

to write a custom error message for a callback function, as long as you substitute 'rule' for the name of the callback function, like such:

Code:
$this->validation->set_message('callback function name', 'error message');
#3

[eluser]CARP[/eluser]
Wow
Thanks charlie spider. That was a very nice/good explanation about that
I thought that I'd be able to create a custom error message for each field easily, but now I see I'll have to translate the english lang. file, etc. or to do 1 callback function for each field...
#4

[eluser]Chillahan[/eluser]
I actually use a bit of a hack which works fine, and gets around having to create individual callback functions for each form field (which as you note is annoying since then you'd be essentially doing all your own data validation anyway!).

All I do is check for the errors I want to set custom messages for at the beginning of the code block that runs if validation is false. Here is a simple example:

Code:
if ($this->validation->run() == FALSE)
        {
            // set custom validation error messages
            if ( $this->validation->agreement_error != '' )
            {
                $this->validation->agreement_error = '<font color="red">' . 'You must check the box to indicate your agreement.' . '</font><br />';
            }

You could of course do a partial string match if you want to only change specific rules' messages (like required vs. alpha vs. valid_email).

DO note that this will bypass the error string start and ending delimiters that you may have custom set, so you will need to add those as I do above. (This is my testing setup, in production I'd use a <div> tag with its own style around all error messages.)

Hope this helps! It sure annoyed the heck out of me too when I saw that promising set_message() function and then discovered it was too universal (except in the context of custom callback functions, for which it's great).
#5

[eluser]indocoder[/eluser]
read this thread too.... http://ellislab.com/forums/viewthread/84970/
hope it helps you..




Theme © iAndrew 2016 - Forum software by © MyBB