Skip to content

Week

Edit this page

7.2

The Week constraint was introduced in Symfony 7.2.

Validates that a given string (or an object implementing the Stringable PHP interface) represents a valid week number according to the ISO-8601 standard (e.g. 2025-W01).

Applies to property or method
Class Week
Validator WeekValidator

Basic Usage

If you wanted to ensure that the startWeek property of an OnlineCourse class is between the first and the twentieth week of the year 2022, you could do the following:

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

use Symfony\Component\Validator\Constraints as Assert;

class OnlineCourse
{
    #[Assert\Week(min: '2022-W01', max: '2022-W20')]
    protected string $startWeek;
}

This constraint not only checks that the value matches the week number pattern, but it also verifies that the specified week actually exists in the calendar. According to the ISO-8601 standard, years can have either 52 or 53 weeks. For example, 2022-W53 is not valid because 2022 only had 52 weeks; but 2020-W53 is valid because 2020 had 53 weeks.

Options

min

type: string default: null

The minimum week number that the value must match.

max

type: string default: null

The maximum week number that the value must match.

groups

type: array | string default: null

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

invalidFormatMessage

type: string default: This value does not represent a valid week in the ISO 8601 format.

This is the message that will be shown if the value does not match the ISO 8601 week format.

invalidWeekNumberMessage

type: string default: The week "{{ value }}" is not a valid week.

This is the message that will be shown if the value does not match a valid week number.

You can use the following parameters in this message:

Parameter Description
{{ value }} The value that was passed to the constraint

tooLowMessage

type: string default: The value should not be before week "{{ min }}".

This is the message that will be shown if the value is lower than the minimum week number.

You can use the following parameters in this message:

Parameter Description
{{ min }} The minimum week number

tooHighMessage

type: string default: The value should not be after week "{{ max }}".

This is the message that will be shown if the value is higher than the maximum week number.

You can use the following parameters in this message:

Parameter Description
{{ max }} The maximum week number

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