Home > OS >  Custom Validation or extended validation
Custom Validation or extended validation

Time:10-12

I have 2 fields that need to be compared against each other but field 2 is not always present so my update validation fails:

return [
    'start_filling'  => 'nullable|date|before:end_filling',
    'end_filling'    => 'nullable|date|after_or_equal:start_filling'
];

How can I set the condition so that before:end_filling only validates when end_filling is present and set.

CodePudding user response:

You can use Rule class for complex conditions.

use Illuminate\Validation\Rule;

return [
    'start_filling'  => ['nullable', 'date', Rule::when($this->end_filling, 'before:end_filling')],
    'end_filling'    => 'nullable|date|after_or_equal:start_filling'
];
  • Related