I'm trying to validate the date fields so that active_from needs to be a date before active_until and active_until needs to be a date after active_from.
Both fields are hidden and disabled until the user selects Yes on a select field called active.
When the user selects Yes, the active_from appears and becomes required and the active_until also appears but it's optional and here lies my problem because the validation fails whenever the active_until is not filled.
From what I've understood, if the field may or may not be enabled/exists I should use the sometimes rule which will only check the other validations if the field exists and is enabled. For e.g.
'active_from' => 'sometimes|required_if:active,yes|date|before:active_until',
The nullable rule should be used if a field may or may not be filled but should it also be used if the field may or may not exist? For e.g.
'active_until' => 'sometimes|nullable|date|after:active_from',
So my question is how can I check if active_from is before active_until only if active is selected to Yes and only if active_until is filled.
Am I using the rules correctly, should active_from only be using the sometimes rule or should it also be using the nullable rule?
Code:
$validated = $request->validate([
'title' => 'required|string|min:3|max:30|unique:categories',
'description' => 'nullable|string|min:3|max:255',
'active' => 'required|in:yes,no',
'active_from' => 'sometimes|required_if:active,yes|date|before:active_until',
'active_until' => 'sometimes|nullable|date|after:active_from',
'highlighted' => 'sometimes|required_if:active,yes|in:yes,no',
'highlighted_from' => 'sometimes|required_if:highlighted,yes|date|before:highlighted_until',
'highlighted_until' => 'sometimes|nullable|date|after:highlighted_from',
]);