I'm very new to Laravel. So please go easy on me.
I'm having this message for "before:18 years ago" date validator.
The "" must be a date before 18 years ago.
How can I customize it ? Do I need to compile something ?
I have this in my validation.php which is in the resources/lang/en/
'custom' => [
'dob' => [
'before' => 'Invalid Age'
],
],
I'm using a FormBuilder
https://pyrocms.com/help/developer-tools/form-builders/creating-a-form-builder
'dob' => [
'type' => 'anomaly.field_type.datetime',
'config' => [
"mode" => "date",
"date_format" => "j F, Y",
"year_range" => "-100:-18",
],
'rules' => [
'required','before:18 years ago'
],
],
You can customize validation messages by specifying attributes within the message text:
$messages = [
'dob' => 'The :attribute must be a date before 18 years ago.',
];
When passed to a validator instance the :attribute
placeholder will be substituted.
$validator = Validator::make($input, $rules, $messages);
Using language files, you define the message text the same:
'custom' => [
'dob' => [
'before' => 'The :attribute must be a date before 18 years ago.'
],
],
and then optionally give the attribute a custom name:
'attributes' => [
'dob' => 'date of birth',
],
You may retrieve lines from language files using the __ helper function. The __ method accepts the file and key of the translation string as its first argument.
I solved it as the following
In FormBuilder
'dob' => [
'type' => 'anomaly.field_type.datetime',
'rules' => ['required', 'older_than'],
'config' => [
'mode' => 'date',
'date_format' => 'j F, Y',
'year_range' => '-100:-18',
],
],
In app/Providers/AppServiceProvider.php
public function boot()
{
Validator::extend(
'older_than',
function ($attribute, $value, $parameters, $validator) {
$minAge = (!empty($parameters))
? (int) $parameters[0]
: 18;
return (new DateTime)->diff(new DateTime($value))->y >= $minAge;
}
);
Validator::replacer(
'older_than',
function ($message, $attribute, $rule, $parameters) {
return "The age must be at least {$parameters[0]}";
}
);
}