uluumbch's avatar

uluumbch was awarded Best Answer+1000 XP

5mos ago

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',
]
uluumbch's avatar

uluumbch wrote a reply+100 XP

5mos ago

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',
]
uluumbch's avatar

uluumbch wrote a reply+100 XP

5mos ago

well I don't think thats necessary because this is not related to what I meant. but to clarify the example invalid data is when user input 29/2020 because it should be in format MM/YYYY so the valid data is like 12/2020 or 08/2024 etc

uluumbch's avatar

uluumbch started a new conversation+100 XP

5mos ago

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.

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.

uluumbch's avatar

uluumbch liked a comment+100 XP

5mos ago

@jegramos

'rules.Illuminate\Validation\Rules\Enum' => 'Valid role values are regular, admin, super-admin.'