vlucas / valitron

Valitron is a simple, elegant, stand-alone validation library with NO dependencies
BSD 3-Clause "New" or "Revised" License
1.57k stars 250 forks source link

Custom Rule error message #199

Open majidakbari opened 7 years ago

majidakbari commented 7 years ago

Hi, How can I add custom message for new custom validation rules in lang files? I mean instead of hard coding the new message, the code it self should be able to read this message from language files.

$this->validator->addRule('unique', function ($field, $value, $params, $fields) {
return true;
}, 'unique' );

And the language file should be look like this:

return [
'required' => 'should be required', 
'unique' => 'the field should be unique'
]
willemwollebrants commented 7 years ago

As far as I know, there isn't really a good way too do this with valitron right now. The language system is pretty limited at the moment: there's no way to load language files from a different location*, or to override the default messages for built-in rules. I'm going to tag this as an "enhancement", because the language (or rather 'labels') system could use some tlc in V2

edit: * this is wrong: using the 3th and 4th parameter of the constructor, you can load language files from a different location