I am stuck in validation in Laravel.
$validatedData = $this->validate($request, [
"date1" => "nullable",
'date2' => 'nullable|after:date1',
]);
Sometimes, both date1
and date2
will be nullable
. If I select date2 then date1 could not be null and date 2 should be greater than date1.
TIA
CodePudding user response:
You can use the required_with rule to validate a field X to be required based on field Y.
Usage: required_with:field1,field2,...
. At least 1 field is required. Where field1 can be any input parameter with which you want to check the field with.
Updated code:
$validatedData = $this->validate($request, [
'date1' => 'nullable|date|required_with:date2',
'date2' => 'nullable|date|after:date1'
]);
Explanation:
Here the input field date1
will be required if the date2
field has some value and is validated successfully, based on the validation rules passed to it.