Search code examples
laraveldatelaravel-validation

Checking date_start and date_end


I have a start date and a end date in a form...

enter image description here

In my method rules I have this:

    public function rules()
    {
            return [
                //
                'date_revision_start' => 'after:2009-01-01|before:2023-01-01',
                'date_revision_end' => 'after:2009-01-01|before:2023-01-01'
             ];
    }

The end date cannot be before the start date.Where should I make the condition? In the Controller ?

    if ($this->request->get('date_revision_start') < $this->request->get('date_revision_end')) 
    {
               return redirect()->route('revisions.index')
                    ->with('error', 'The end date cannot be before the start date!');
    }

Solution

  • You no need to check condition if you are using validation

    public function rules()
    {
        return [
            //
            'date_revision_start' => 'after:2009-01-01|before:2023-01-01',
            'date_revision_end' => 'after:date_revision_start|before:2023-01-01'
        ];
    }