Welcome Guest, Not a member yet? Register   Sign In
Custom validation error messages per rule
#21

[eluser]Khoa[/eluser]
Is it a bit redundant when we need both the function and the rule for each checking to work? Can we make it work with just the $lang['required_custom'] itself but no need for the function required_custom($str)?
#22

[eluser]xwero[/eluser]
the line is related to the method. If there is no method the line is fat for the language file.

You can go without the line but then the easiest way is to put the string in the set_message method. This is less flexible as you hard code the string then. You can create another language file to put your custom lines in en load it when you need to use the set_message method.

I wonder why you find it so strange that you need a function and a language line because that is how the CI default rules work as well.
#23

[eluser]Khoa[/eluser]
Ye, I know that inside the builtin validation class, there is 1 function for each of the rule like function required, function max_length...which is reasonable because this is the first time such rule is set up. So we definitely need to have a place to say: how to check for "required", how to check for "max_length", etc.

But when we set up our custom rules, that's differernt story. The rules are there, we just call it, but instead of returning the default error message, we want it to return our custom message. So, what I'm THINKING (just thinking, not coding yet :-P) is to have sth like this:

Code:
$rules['title'] = 'customValidate[required,title]|customValidate[max_length[100],title]';
$rules['description'] = 'customValidate[required,description]|customValidate[max_length[2000],description]';

//Then there will be a function inside the Validation class (or maybe extend it, not sure) like this:
function customValidate($str, $ruleFunction)
{
//Extract the rule parameters from the rule function above.
//That is to get 100 out of max_length[100]. Let's say we now have:
$param = 100;

//Get the result from the built-in function
$result = $this->$ruleFunction($_POST[$field], $param);

//In case it fails, get the custom error message instead of the built-in one
if ($result == FALSE)
{
//return $lang[$ruleFunction][$_POST[$field]]
}

return TRUE;
}

On the line of $rules['title'] =... we can replace the customValidate by sth else to indicate that this is our custom function but not the builtin on, sth like: _max_length[100, title], and inside the code we check if the function starts with "_", we will redirect it to the customValidate function. This helps reduce the amount of typing but still giving the function a meaningful name inside the validation class.

The main advantage of this (well, if it ever works!) is when we have another field to validate, we just need to add another line into the lang file saying $lang['required']['newField'] = ... and write $rules['newField'] = _required[newField].

Code:
$rules['title'] = '_required[title]|_max_length[100,title]';
$lang['required']['title'] = 'Oops! You forgot your title';

This is just a prototype anyway :-)
#24

[eluser]xwero[/eluser]
Now you are talking about an better/easier way to set custom messages. I would even push it further and reserve the first parameter of every rule, even callbacks for a custom message.
Code:
$rules['field1'] = 'max_length[,100]'; // no custom message
$rules['field2'] = 'max_length[custom_msg,100]';
$rules['field3'] = 'required'; // no param no square brackets
$rules['field4'] = 'required[custom_msg]';
$rules['field3'] = 'callback_method[custom_msg]';
$rules['field4'] = 'callback_method[custom_msg,more,params]';
The custom_msg in the examples are the line keys.

This is possible because the rule is split from the params before the rule is called.
#25

[eluser]Khoa[/eluser]
Ye, that works for me too. All I think important is: able and easy to add custom message with least code and least customization of the builtin class. If you put the custom message as a parameter, does it mean you have to customize every built-in function such as required($str), max_length($str) and so on to make sure that the code will take the parameters starting from position 1 instead of 0? I know this is not a big deal (isn't it?) but then what happens when we upgrade to the next version of CI? Please advise.
#26

[eluser]xwero[/eluser]
You don't have to overwrite the rules to do what i propose. You only need to change the run method. Where the parameters are fetched you need to check if a custom message is added or not. If a custom message is added you need to check if the line exists and if it does display it.

I would advise against setting the custom message as second parameter because then you have to do things like
Code:
$rules['field4'] = 'required[,custom_msg]';
$rules['field4'] = 'required[param1,custom_msg,param2]';

As far as updating CI is concerned if you put your code in the MY_Validation.php file you can wait as long as you want to use your solution.
#27

[eluser]Khoa[/eluser]
Hmm. I see. I will give it a go, and will share it here when I have it work. Thanks a lot for all your time and patience xwero ;-)




Theme © iAndrew 2016 - Forum software by © MyBB