Example of adding a validation rule when updating Laravel code

For cases where the update method is only slightly different from the create method, you can extend the form request of the method and modify the rules for update. Additionally, you can turn the rules method into an abstract class, which can then be extended by both create and update form request classes.
Alternatively, if the create and update logic is distinct, you may use different validation rules for each method.
Assuming that the validation rule is placed on HttpRequest, you can modify it to fit your needs. Additionally, you can include the controller in your modifications.
Another solution involves creating various validation methods for either create or update, then applying the necessary validation depending on the condition.
Finally, you can create a class that extends the aforementioned method. More information on this can be found in the official documentation.


Solution 1:

It is recommended to have separate form request classes for creating and updating events, but this does not mean that inheritance cannot be utilized.

If your method for updating has minor variations from the method for creating, you can opt to expand the form request of the latter and modify the update rules accordingly.

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

One option is to make the rules method an abstract class and have the create and update form request classes extend it.

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
    }
}


Solution 2:


To handle different create and update logic, one can simply utilize distinct validation rules for each method.

'event_start' => 'required|date|after:today', // For creating
'event_start' => 'required|date', // For updating


Solution 3:


Assuming that the validation rule is present on the HttpRequest, one could modify it to become:

'event_start' => 'required|date',

And on your controller:

public function store(EventRequest $request)
{
       $validator = Validator::make($request->all(), [
            'event_start' => 'date|after:today',
       ]);
       if($validator->fails())
       {
           return redirect('event/create')
                    ->withErrors($validator)
                    ->withInput();
       }
       // Save the event
}

Frequently Asked Questions