简体   繁体   中英

Laravel: How can I validate two optional date fields using the before and after validation rules

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 eg

'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 eg

'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',
]);

I was able to finally figure it out.

This is my fixed 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' => 'required_if:active,yes|date',
    'active_until' => 'nullable|date|after:active_from',
    'highlighted' => 'required_if:active,yes|in:yes,no',
    'highlighted_from' => 'required_if:highlighted,yes|date',
    'highlighted_until' => 'nullable|date|after:highlighted_from',
]);

Instead of checking both dates being that one is optional even if the user selects Yes on the active select, I just need to check the active_until date (the optional one) to see if it is a valid date after the active_from date.

This way, even if the user doesn't fill the active_until date, the validation won't fail because I have the nullable rule on that field.

As for the sometimes rule, from what I've understood it only needs to be used if a field may or may not exist in the request for eg

'active' => 'sometimes|required|in:yes,no',

This way, it's only required if it's present in the request but since I'm using the required_if on my code, it's not necessary because it depends on the value of the other field.

For complex validaion rules

    use Illuminate\Support\Facades\Validator; 

    .... 
    $data = $request->all();
    $validator = Validator::make($data,[
       //...your unconditional rules goes here
    ]);

    //your conditional rules goes here
    $validator->sometimes('active_form', 'before:active_until', function ($request) {
        return $request->filled('active_until');//if here return true,rules will apply
    }); 

Important link
conditionally-adding-rules
retrieving-input

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM