In laravel 4 you can create custom validation error message by adding it to $this->messages
array. But in my case, I need to add it at runtime, when a custom validation rule is processing.
For example I have such custom rule:
Validator::extend('isCityNamesValid', function ($attribute, $value, $parameters) {
$value = array_map('trim', explode("
", $value));
if(empty($value)) {
return true;
}
$invalidNames = \City::getInvalidCityNames($value, $parameters);
if(!empty($invalidNames)) {
$this->messages[$attribute.'.is_city_names_valid'] = sprintf('You provide invalid city names for selected countries, please remove/fix these names: %s.', implode(', ', $invalidNames));
return false;
}
return true;
});
In this custom rule, I'm creating message only when the rule is failing and I'm appending custom data to the message. Is it possible without overriding default laravel passes
method? or I need to make such validation without using laravel validation mechanism?