2
votes

I have two input date in a form, one of them is start_date and the other is end_date and I am trying to do a validation that verifies that end_date is required if start_date has already been entered.

I did that part, but now I can't add the other checks because it doesn't work as it should. If I use "date_format: Y-m-d" the field always becomes required.

If I want to use "after_or_equal:" then the field always returns error if start_date is null.

return [
        "start_date" => "nullable|date_format:Y-m-d",
        "end_date" => new RequiredIf($this->start_date !== NULL).'|date_format:Y-m-d|after_or_equal:start_date',
    ];

That is my validator, if I delete the validation of the "date_format" and "after_or_equal" it works, but I need to validate all those data

2

2 Answers

2
votes

you have to use the nullable rule first and then use required_with and the rest rules.

$this->validate($request, array(
    'start_date' => "nullable|date_format:Y-m-d",
    'end_date' => 'nullable|required_with:start_date|date_format:Y-m-d|after_or_equal:start_date',
));
0
votes

Try something like below it will solve your problem

return [
        "start_date" => "nullable|date_format:Y-m-d",
        "end_date" => "required_if:start_date,after_or_equal:start_date|nullable|date_format:Y-m-d",
    ];