'data.formalEducations.*.start_month_and_year.App\Rules\MonthYearValue.invalid' => 'Only show this message when invalid.',
When invalid what? Can you show invalid data which should produce this error message? And other applied rules also.
I create a custom validation using Class that implements ValidationRule. it works perfectly, the problem is I can't use custom error message just like other laravel native rule.
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Validation\Validator;
use Illuminate\Contracts\Validation\ValidatorAwareRule;
use Illuminate\Contracts\Validation\DataAwareRule;
class MonthYearValue implements ValidationRule, ValidatorAwareRule,DataAwareRule
{
/**
* The validator instance.
*
* @var \Illuminate\Validation\Validator
*/
protected $validator;
/**
* All of the data under validation.
*
* @var array<string, mixed>
*/
protected $data = [];
/**
* Set the current validator.
*/
public function setValidator(Validator $validator): static
{
$this->validator = $validator;
return $this;
}
public function setData(array $data): static
{
$this->data = $data;
return $this;
}
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (! is_string($value)) {
$fail("Format $attribute tidak valid.");
return;
}
$value = trim($value);
if (! preg_match('/^(0[1-9]|1[0-2])\/\d{4}$/', $value)) {
$fail('validation.month_year')->translate([
'attribute' => $attribute,
]);
}
}
}
In example above, I can display the message via validation translate file
<?php
return [
'month_year' => 'The :attribute must be a valid month/year (MM/YYYY).',
];
But, what exactly my goal is to use custom error message if provided.
Here is what work for now,
'data.formalEducations.*.start_month_and_year.App\Rules\MonthYearValue' => 'Bulan dan tahun mulai tidak valid. Gunakan format MM/YYYY.',
that work because I specify namespace to my Class, but then I can't display correct message based on condition. for example I want it to display based on some rule
'data.formalEducations.*.start_month_and_year.App\Rules\MonthYearValue.invalid' => 'Only show this message when invalid.',
for now, I don't have idea how to check that invalid to show correct error message value.
I found a solution for this, apparently we need to use Validator::extend() to make an alias for our Validation Class.
in AppServiceProvider or another provider you have
Validator::extend('month_year', function ($attribute, $value, $parameters, $validator) {
$rule = new MonthYearValue;
// Optionally inject data / validator if needed:
$rule->setData($validator->getData());
$rule->setValidator($validator);
$failed = false;
$rule->validate($attribute, $value, function ($attr, $message = null) use (&$failed, $validator) {
$failed = true;
// Add message; we can translate or store code
$validator->errors()->add($attr, $message ?? 'Invalid month/year.');
});
return ! $failed;
}, 'Format bulan dan tahun tidak valid. Gunakan MM/YYYY.');
with that, now we can use the alias in the validation method.
$request->validate([
'data.formalEducations.*.start_month_and_year' => ['required', 'month_year'],
],[
'data.formalEducations.*.start_month_and_year.month_year' => 'Now this custom error message will displayed if exist',
]
Please or to participate in this conversation.