0

I'm trying to build an application in Laravel where I need to take input user's job experience with their start date and end date. There is a use case where the user can be currently working in any organization, so there will not be any end_date. I need to skip end_date in case there is no input or check currently_working checkbox (trying Boolean value) validate the user inputs, my validation rule look something like this:

public function rules()
{
    return [
        'employeee_type'=>'required',
        'company_name'=>'required',
        'location' =>'required',
        'start_date' => 'required|date|before:end_date',
        'end_date'=> Rule::excludeIf(fn () => request('currently_working')).'sometimes|date|after:start_date',
    ];
}

Even though I'm passing empty value to end_date with currently_working as false, I'm getting error

0: "The end date is not a valid date."

1: "The end date must be a date after start date."

How can I fix this, any help is appreciated. Thanks

0

3 Answers 3

0

You can use "required_if" validation and add "currently_working" in your request

public function rules()
{
    return [
        'employeee_type'=>'required',
        'company_name'=>'required',
        'location' =>'required',
        'start_date' => 'required|date|before:end_date',
        'currently_working ' => 'required|boolean',
        'end_date'=> 'required_if:currently_working ,==,false"|date|after:start_date',
    ];
}

link to laravel doc here

Sign up to request clarification or add additional context in comments.

1 Comment

This will check whether the currently_working input has the string value "false"
0

excludeIf() does not exclude the value from validation, it only excludes the value from the resulting array of validated data. You can read documentation for more details. In addition, you can't just append the output of the Rule functions to a string.

A checkbox is not included at all in a post if it isn't checked, so you can simply apply the rule if it's present in the request. It's not documented for some reason, but in a FormRequest class, this is done using the withValidator() method:

use Illuminate\Support\Fluent;
use Illuminate\Validation\Validator;

/**
 * Get the validation rules that apply to the request.
 */
public function rules(): array
{
    return [
        'employeee_type' => 'required',
        'company_name' => 'required',
        'location' => 'required',
        'start_date' => ['required', 'date', 'before:end_date'],
    ];
}

/**
 * Configure the validator instance.
 */
public function withValidator(Validator $validator): void
{
    $validator->sometimes(
        'end_date',
        ['date', 'after:start_date'],
        fn (Fluent $input) => is_null($input->get('currently_working'))
    );
}

Comments

-1

To perform Laravel date validation based on whether a checkbox is selected or not, you can use custom validation rules

if ($this->checkboxValue) {
        // Add your date validation logic here
        // For example, to validate a date in the format YYYY-MM-DD, you can use:
        return \Illuminate\Support\Facades\Date::createFromFormat('Y-m-d', $value) !== false;
    }

Use the custom validation rule in your Controller Now, in your Laravel Controller that handles the form submission, you can use the custom validation rule like this:

 $request->validate([
    // Other validation rules for other fields
    'selected_date' => ['nullable', new ConditionalDateValidation($request->input('is_date_required'))],
]);

Comments

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.