[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 :-)