Validation
Validation copied to clipboard
how to add custom messages in key/value method and change message format
Hi,
This is my first test with library and I'm quite impress with it.
use Respect\Validation\Validator as v;
use Respect\Validation\Exceptions\NestedValidationException;
$data = [
'name' => 'bon jovi',
'age' => '32',
'sex' => 'male',
'address' => '',
];
$messages = [
'length' => '{{name}} custom length message',
'between' => '{{name}} custom between message',
'in' => '{{name}} custom in message',
];
try {
v::key('name', v::stringType()->length(1, 5)->setName('Name'))
->key('age', v::intType()->between(40, 50))
->key('sex', v::stringType()->in(['female','male']))
->key('address', v::stringType())
->key('hello', v::stringType())
->assert($data);
} catch (NestedValidationException $ex) {
print_r($ex->getMessages($messages));
}
I'm more interested in KeyValue validation. In the code above there is no use of passing custom error messages to getMessages to replace default messages. Is there any way to pass custom messages in KeyValue method? I have seen couple of examples in documentation but I don't think you can create custom message specifically for a particular key and validation type. For e.g. name.length
Secondly, I would like to change the format of messages in KeyValue method. Here is the format I would prefer:
name => {'length'=>'length message', 'between'=>'between message'},
age => {'between'=>'between message'}
This format is more useful when dealing with form validation to take appropriate actions base on validation type also.
Hi @prashant-saxena
Your code is almost perfect, but you need to use the setName method for each key you define.
After defining each value in your keys, you reference these keys in your custom array that will be passed to the getMessages method
Example
$data = [
'name' => 'bon jovi',
'age' => '32',
'sex' => 'male',
'address' => '',
];
$messages = [
'length' => '{{name}} custom length message',
'between' => '{{name}} custom between message',
'in' => '{{name}} custom in message',
];
try {
v::key('name', v::stringType()->length(1, 5)->setName('length'))
->key('age', v::intType()->between(40, 50)->setName('between'))
->key('sex', v::stringType()->in(['female','male'])->setName('in'))
->key('address', v::stringType())
->key('hello', v::stringType())
->assert($dta);
} catch (NestedValidationException $ex) {
print_r($ex->getMessages($messages));
}
My question then is how can I set a custom message for one of the other validators. Let's take 'name' as example. It has 'stringType' and 'length' validators. How can I provide a custom message also for 'stringType'?
As in the given example code setName() is not needed. The $messages array could simply define a custom message for 'name' key/value:
$messages = [
'name' => '{{name}} custom length message',
'age' => '{{name}} custom between message',
'sex' => '{{name}} custom in message',
];
But how to define custom message for 'stringType' and 'length'?
Same question here, danilocorrea87 have a point but this way is only ona general message for each input and can not be clear with error source because this message will be like "Name error please verify".
In older versions there was a way to personalize each rule's message, messages we want to set are like : "Name must have 13 letter" "Name can not have numbers" etc
ther is a way to do that?
try
$validator = new Validator();
$validator->attribute('name', Validator::allOf(
Validator::alpha()->setTemplate('Name can not have numbers'),
Validator::length(13,null)->setTemplate('Name must have 13 letter')
));
The example from @rodrigoaramburu should work for that. I'm closing this. If any questions are still remaining, please reopen this issue!