Search code examples
phplaravel

Laravel - validation rule for date on update


I have this validation rule:

'event_start' => 'required|date|after:today',

and on creating a model all is fine, this date cannot be before today....

but when a user tries to update the event start date is before today and a validation error pops up....is there a way to adjust this for updating the model? So that when a user updates this rule checks only if it is a date?

UPDATE

this is my only validation check for both create and update:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class EventsForm extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        if( auth()->check() )
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [

            'title' => 'required|min:2|max:255',
            'event_start' => 'required|date|after:today',

        ];

        return $rules;
    }
}

Solution

  • You should have different form request classes for creating and updating events. That’s not to say you can’t use inheritance, though.

    If your update method only slightly differs from the create method, then you could extend that method’s form request and amend the rules for update:

    class CreateEventRequest extends FormRequest
    {
        public function authorize()
        {
            return true;
        }
    
        public function rules()
        {
            return [
                'name' => 'required|unique:events',
                'start_date' => 'required|date|after:today',
                'door_time' => 'required|date_format:"H:i:s",
                'location' => 'required',
            ];
        }
    }
    

    And…

    class UpdateEventRequest extends CreateEventRequest
    {
        public function rules()
        {
            // Get ID of event if using route–model binding
            $id = $this->route('event')->getKey();
    
            // Use array merge to override create event form request’s rules
            return array_merge(parent::rules(), [
                'name' => 'required|unique:events,name,'.$id,
                'start_date' => 'required|date',
            ]);
        }
    }
    

    You could even push the rules method to an abstract class, and have your create and update form request classes extend that class:

    abstract class EventFormRequest extends FormRequest
    {
        public function authorize()
        {
            return true;
        }
    
        public function rules()
        {
            // Base event rules
        }
    }
    
    class CreateEventRequest extends EventFormRequest
    {
        public function rules()
        {
            // Create-specific rules
        }
    }
    
    class UpdateEventRequest extends EventFormRequest
    {
        public function rules()
        {
            // Update-specific rules
        }
    }