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.
CodePudding user response:
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',
]);