skip to Main Content

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

2

Answers


  1. 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

    Login or Signup to reply.
  2. public function rules()
    {
        return [
            'employeee_type'=>'required',
            'company_name'=>'required',
            'location' =>'required',
            'start_date' => 'required|date|before:end_date',
            'end_date'=> 'required_if:currently_working,false|date|after:start_date',
        ];
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search