Skip to content

Cron

Edit this page

Validates that a value is a valid cron expression. By default, it accepts standard five-field expressions (e.g. 0 6 * * 1-5) and predefined scheduling aliases (e.g. @daily).

Note

Using this constraint requires having the dragonmantank/cron-expression package installed in your application (e.g. by running composer require dragonmantank/cron-expression).

Applies to property or method
Class Cron
Validator CronValidator

8.2

The Cron constraint was introduced in Symfony 8.2.

Basic Usage

The following example ensures that the schedule property holds a valid cron expression:

1
2
3
4
5
6
7
8
9
10
// src/Entity/ReportTask.php
namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class ReportTask
{
    #[Assert\Cron]
    public string $schedule;
}

Note

As with most of the other constraints, null and empty strings are considered valid values. This is to allow them to be optional values. If the value is mandatory, a common solution is to combine this constraint with NotBlank.

Options

groups

type: array | string default: null

It defines the validation group or groups of this constraint. Read more about validation groups.

message

type: string default: This value is not a valid cron expression.

This message is shown if the value is not a valid cron expression.

You can use the following parameters in this message:

Parameter Description
{{ value }} The current (invalid) value

mode

type: string default: Cron::MODE_ALIASES

This option defines which cron expression formats are valid. The possible values are available as constants in the Cron class:

  • Cron::MODE_ALIASES accepts standard five-field expressions and predefined scheduling aliases such as @yearly, @monthly, @weekly, @daily and @hourly;
  • Cron::MODE_STANDARD accepts only standard five-field expressions and rejects predefined aliases;
  • Cron::MODE_HASHED accepts the same values as Cron::MODE_ALIASES and the hashed cron expressions used by the Scheduler component (e.g. # # * * * or #daily).

payload

type: mixed default: null

This option can be used to attach arbitrary domain-specific data to a constraint. The configured payload is not used by the Validator component, but its processing is completely up to you.

For example, you may want to use several error levels to present failed constraints differently in the front-end depending on the severity of the error.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version