Laravel 5 Cron expression validation

A Cron expression validator is created in Laravel 5.3. Laravel provides a versatile and extendable Validation class. Introducing new validations is done by registering a validation function with the extend method on the Validation facade. A Cron expression validator is created by utilising a cron-expression parser.

The cron-expression parser used here is the fantastic cron-expression Composer package by Michael Dowling.

First, require the Composer package.

Second, extend the Validator-class with a cron_expression validation method in the boot method of an appropriate Service Provider (e.g. AppServiceProvider.php). Don’t forget to import the class:

Then modify the boot function.

It is also possible to put this in a separate package (and separate Service Provider).

Finally, add the translation to the appropriate language files (e.g. /resources/lang/en/validation.php):

Use the validator like any other validation rule.

Done already! I enjoy the ease of implementing a new validation rule in Laravel.

Sponsored content