-2
Greetings,
I’m having trouble making a validation by Laravel. Today, I need to do the following validation
'data_visita' => ['required', 'date_format:d/m/Y', new AfterOrEqual1900],
when you’re alone ['required', 'date_format:d/m/Y']
the system tries the validation normally, checks if it is mandatory and if it is in the specified format. However, when I add the validation new AfterOrEqual1900
the second validation, 'date_format:d/m/Y'
is ignored and goes straight to the last. Someone can let me know what happens?
The Afterorequal1900 rule is,
Anyway, the code for Afterorequal1900 is,
public function passes($attribute, $value)
{
$timezone = 'America/Sao_Paulo';
$min_date = Carbon::create(1900, 1, 1, 0, 0, 0, $timezone);
$date_of_visit = new Carbon(Carbon::createFromFormat('d/m/Y', $value, $timezone));
// importante zerar para comparação sair corretamente
$date_of_visit->second = 0;
$date_of_visit->minute = 0;
$date_of_visit->hour = 0;
return $date_of_visit->gte($min_date);
}
explain to me what you want to validate in this validation
– Bulfaitelo
I need to check if the date is before 01/01/1900. Then I check if a value has been passed, if the date is in this dd/mm/yyyy format and, finally, as I couldn’t find another way to compare, I created a custom rule.
– user2449058
AfterOrEqual1900
puts this method in the question for me to analyze, as it being ignored, may be its logic the problem.– Bulfaitelo
The method
AfterOrEqual1900
is not ignored. The problem is that it is analyzed before the'date_format:d/m/Y'
.– user2449058
if you leave only this validation
AfterOrEqual1900
she behaves as she should ?– Bulfaitelo
I think this may clarify a few things https://stackoverflow.com/questions/20692792/how-validation-rules-work-in-laravel
– Bulfaitelo
Now I know what was wrong. Laravel does the validations without using the short-Circuit evaluation, that is, it will check all the rules, before returning, regardless of whether an error occurred or not. To force short-Circuit behavior it is necessary to prefix with
bail
the validation rules. I thank you for the strength.– user2449058