Reputation: 1095
I've made a custom validator, that compares two dates, and I want to show a message to the user which says the one date (the invoicedate
in my example) must be earlier than the other one (the deadline
in my example).
Inside my custom validator I write:
public static validateInfo(Request $request)
{
$validator = Validator::make($request->all(), [
'referencenumber' => 'required|min:2|max:64',
'invoicedate' => 'required|date',
'deadline' => 'null|date'
]);
$validator->after(function ($validator) use ($request) { // custom static function where I compare two dates using strtotime(), if invoicedate and deadline are still valid, and return false or true
if (InvoiceValidator::invalidDeadline($validator, $request)) {
$validator->errors()->add('deadline', __('validation.negative_date_difference', [
'attribute1' => 'deadline',
'attribute2' => 'invoicedate'
]));
}
});
return $validator;
}
And inside resources\lang\en\validation.php
I write:
<?php
return [
// ...
'negative_date_difference' => 'The :attribute1 may not be earlier than the :attribute2.',
// ...
'attributes' => [
'referencenumber' => 'Reference Number', // This works. It's fine
'invoicedate' => 'Invoice Date' // But this does not work, of course, because I wrote $validator->errors()->add('deadline'...); so the deadline is the only targetted attribute name here
],
]
Current output is:
The Deadline may not be earlier than the invoicedate.
My question: how to bypass invoicedate
, when this is the message I want to see?
The Deadline may not be earlier than the Invoice Date.
Upvotes: 0
Views: 80
Reputation: 1095
Inside resources\lang\en\messages.php
I've added a new message:
<?php
return [
'invoice_date' => 'Invoice Date'
]
Then edited my custom validation function, as follows:
if (InvoiceValidator::invalidDeadline($validator, $request)) {
$validator->errors()->add('deadline', __('validation.negative_date_difference', [
'attribute1' => 'deadline',
'attribute2' => __('messages.invoice_date') // This works
]));
}
Upvotes: 1