AtLeastOneOf
此约束检查值是否满足至少一个给定的约束。一旦满足一个约束,验证就会停止。
应用于 | 属性或方法 |
类 | AtLeastOneOf |
验证器 | AtLeastOneOfValidator |
基本用法
以下约束确保
- Student 的
password
要么包含#
,要么至少10
个字符长; - Student 的
grades
是一个数组,其中包含至少3
个元素,或者每个元素都大于或等于5
。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// src/Entity/Student.php
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class Student
{
#[Assert\AtLeastOneOf([
new Assert\Regex('/#/'),
new Assert\Length(min: 10),
])]
protected string $plainPassword;
#[Assert\AtLeastOneOf([
new Assert\Count(min: 3),
new Assert\All(
new Assert\GreaterThanOrEqual(5)
),
])]
protected array $grades;
}
选项
includeInternalMessages
类型: boolean
默认值: true
如果设置为 true
,则在验证失败时显示的消息将包含内部约束的消息列表。有关示例,请参见 message 选项。
message
类型: string
默认值: This value should satisfy at least one of the following constraints:
这是验证失败时将显示的消息的介绍。默认情况下,它将跟随内部约束的消息列表(可通过 includeInternalMessages 选项配置)。例如,如果上面的 grades
属性验证失败,则消息将是:This value should satisfy at least one of the following constraints: [1] This collection should contain 3 elements or more. [2] Each element of this collection should satisfy its own set of constraints.
messageCollection
类型: string
默认值: Each element of this collection should satisfy its own set of constraints.
这是验证失败时将显示的消息,且内部约束为 All 或 Collection。有关示例,请参见 message 选项。
这项工作,包括代码示例,根据 Creative Commons BY-SA 3.0 许可协议获得许可。