0

I have two datepicker input fields: from and to. To ensure that field value to is greater than field value from, I use the after validation rule. Everything is fine up until I leave the from input value null. Because I am applying the validation rule using the input value from. How can I combine the required and after validation rules without running into this problem?

$from = $request->input('from');
$from = Carbon::createFromFormat('Y-m-d\TH:i', $from)->format('Y-m-d H:i A');
$attributes= request()->validate([
       'from' => 'required|date',
       'to'=> 'nullable|date|after:'.$from,
]);

Data missing error while from input value is empty.

1 Answer 1

1

The laravel validation rule allows you to compare a field against another field. So you can simply add: after:from See the documentation here.

$attributes = request()->validate([
    'from' => 'required|date',
    'to'=> 'nullable|date|after:from',
]);
Sign up to request clarification or add additional context in comments.

1 Comment

I'm wondering how I'm not familiar with that simple solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.