跳到内容

范围

编辑此页

验证给定的数字或 DateTime 对象是否在某个最小值和最大值之间。

应用于 属性或方法
范围
验证器 RangeValidator

基本用法

要验证一个类的高度 (height) 字段是否在 120180 之间,您可以添加以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// src/Entity/Participant.php
namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Participant
{
    #[Assert\Range(
        min: 120,
        max: 180,
        notInRangeMessage: 'You must be between {{ min }}cm and {{ max }}cm tall to enter',
    )]
    protected int $height;
}

日期范围

此约束可用于比较 DateTime 对象与日期范围。范围的最小值和最大值应以 DateTime 构造函数接受的任何日期字符串形式给出。例如,您可以像这样检查日期是否必须在今年之内

1
2
3
4
5
6
7
8
9
10
11
12
13
// src/Entity/Event.php
namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Event
{
    #[Assert\Range(
        min: 'first day of January',
        max: 'first day of January next year',
    )]
    protected \DateTimeInterface $startDate;
}

请注意,PHP 将使用服务器配置的时区来解释这些日期。如果您想固定时区,请将其附加到日期字符串中

1
2
3
4
5
6
7
8
9
10
11
12
13
// src/Entity/Event.php
namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Event
{
    #[Assert\Range(
        min: 'first day of January UTC',
        max: 'first day of January next year UTC',
    )]
    protected \DateTimeInterface $startDate;
}

DateTime 类也接受相对日期或时间。例如,您可以像这样检查交货日期是否在接下来的五个小时内开始

1
2
3
4
5
6
7
8
9
10
11
12
13
// src/Entity/Order.php
namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Order
{
    #[Assert\Range(
        min: 'now',
        max: '+5 hours',
    )]
    protected \DateTimeInterface $deliveryDate;
}

选项

groups

类型: array | string 默认值: null

它定义了此约束的验证组。阅读更多关于 验证组 的信息。

invalidDateTimeMessage

类型: string 默认值: 该值应为有效的数字。

minmax 值是 PHP 日期时间,但给定的值不是时,显示的错误消息。

您可以在此消息中使用以下参数

参数 描述
{{ value }} 当前的(无效)值

invalidMessage

类型: string 默认值: 该值应为有效的数字。

minmax 值是数值(根据 is_numeric PHP 函数),但给定的值不是时,显示的错误消息。

您可以在此消息中使用以下参数

参数 描述
{{ value }} 当前的(无效)值
{{ label }} 对应的表单字段标签

max

类型: numberstring (日期格式)

这个必需的选项是“max”值。如果给定的值大于此最大值,验证将失败。

maxMessage

类型: string 默认值: 该值应为 {{ limit }} 或更小。

如果底层值大于 max 选项,且未定义 min 选项(如果两者都已定义,请使用 notInRangeMessage),则会显示此消息。

您可以在此消息中使用以下参数

参数 描述
{{ limit }} 上限
{{ value }} 当前的(无效)值

maxPropertyPath

类型: string

它定义了对象属性,其值用作 max 选项。

例如,如果您想将某个对象的 $submittedDate 属性与同一对象的 $deadline 属性进行比较,请在 $submittedDate 的范围约束中使用 maxPropertyPath="deadline"

提示

当使用此选项时,其值在错误消息中作为 {{ max_limit_path }} 占位符提供。虽然不打算将其包含在向最终用户显示的错误消息中,但在使用 API 在客户端进行任何映射逻辑时,它很有用。

min

类型: numberstring (日期格式)

这个必需的选项是“min”值。如果给定的值小于此最小值,验证将失败。

minMessage

类型: string 默认值: 该值应为 {{ limit }} 或更大。

如果底层值小于 min 选项,且未定义 max 选项(如果两者都已定义,请使用 notInRangeMessage),则会显示此消息。

您可以在此消息中使用以下参数

参数 描述
{{ limit }} 下限
{{ value }} 当前的(无效)值

minPropertyPath

类型: string

它定义了对象属性,其值用作 min 选项。

例如,如果您想将某个对象的 $endDate 属性与同一对象的 $startDate 属性进行比较,请在 $endDate 的范围约束中使用 minPropertyPath="startDate"

提示

当使用此选项时,其值在错误消息中作为 {{ min_limit_path }} 占位符提供。虽然不打算将其包含在向最终用户显示的错误消息中,但在使用 API 在客户端进行任何映射逻辑时,它很有用。

notInRangeMessage

类型: string 默认值: 该值应介于 {{ min }} 和 {{ max }} 之间。

如果底层值小于 min 选项或大于 max 选项,则会显示此消息。

您可以在此消息中使用以下参数

参数 描述
{{ max }} 上限
{{ min }} 下限
{{ value }} 当前的(无效)值

payload

类型: mixed 默认值: null

此选项可用于将任意特定于域的数据附加到约束。配置的 payload 不会被 Validator 组件使用,但其处理完全取决于您。

例如,您可能希望使用多个错误级别,以便根据错误严重性在前端以不同方式呈现失败的约束。

本作品,包括代码示例,根据 Creative Commons BY-SA 3.0 许可协议获得许可。
目录
    版本