DEV Community

Cover image for Utilizing the Laravel Validator after hook
Bilal Haidar
Bilal Haidar

Posted on

Utilizing the Laravel Validator after hook

In Laravel, Validation is a first-class citizen. The validation API is rich and powerful that can cover almost any validation problem you might have. There are existing validation rules and you can write your own custom ones.

In this blog post, we'll look at how to use the "after" book to run a callback after the Laravel Validation process is completed. This way, you can perform additional validation checks and report about them.

The first step in handling form submissions in Laravel is to create a form request class. Form request classes validate user input and handle any additional logic required for your application.


class StoreEventRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules()
    {
        return [
            'name' => 'required',
            'date' => 'required|date',
        ];
    }

    public function withValidator($validator)
    {
        $validator->after(function ($validator) {

            $date = $validator->safe()->date;

            if(strtotime($date) < strtotime('now')) {
                $validator->errors()->add('date', 'Date should be in the future.');
            }
        });
    }
}
Enter fullscreen mode Exit fullscreen mode

In the code example above, we have a form request class called StoreEventRequest, which is used to validate user input when creating a new event.

The StoreEventRequest class extends the built-in FormRequest class, providing basic functionality for handling form submissions in Laravel.

The most crucial method in this class is the rules() method, which defines the validation rules for the form request. In this case, the form must include a field named name and a field named date, and both fields are required. Additionally, the date field must be a valid date.

The built-in validation system uses the rules() method in Laravel to ensure the user input is valid. If the user input fails validation, an error message will be displayed to the user, and the form request will be considered invalid.

In addition to the rules() method, the StoreEventRequest class contains a method withValidator($validator). This method receives the fully constructed validator, allowing you to call any of its methods before the validation rules are evaluated.

In Laravel, if you want to add an "after" validation hook to a form request, you may use the withValidator() method.

The "after" hook defines an event listener triggered after applying the validation rules. It accepts a validator instance, allowing additional validation logic to be added.

In this case, we are using the "after" hook to apply additional validation on the date route parameter.

Inside the "after" hook callback function, the $validator->safe()->date line is accessing the value of the date field that is being validated.

Then, it uses the strtotime() function to convert the date string into a timestamp and compares it with the current time(now) timestamp.

If the entered date is in the past (i.e., the timestamp of the entered date is less than the timestamp of the current time), the code adds an error message to the validator's error collection using $validator->errors()->add('date', 'Date should be in the future.'). This error message will be displayed to the user if the validation fails.

That's it!

Top comments (0)