AtLeastOneOf ¶
This constraint checks that the value satisfies at least one of the given constraints. The validation stops as soon as one constraint is satisfied.
Applies to | property or method |
Class | AtLeastOneOf |
Validator | AtLeastOneOfValidator |
Basic Usage ¶
The following constraints ensure that:
- the
password
of aStudent
either contains#
or is at least10
characters long;生徒のパスワードに # が含まれているか、少なくとも 10 文字の長さです。 - the
grades
of aStudent
is an array which contains at least3
elements or that each element is greater than or equal to5
.Student の成績は、少なくとも 3 つの要素を含むか、各要素が 5 以上の配列です。
-
Attributes
属性
-
YAML
YAML
-
XML
XML
-
PHP
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// src/Entity/Student.php
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
// IMPORTANT: nested attributes requires PHP 8.1 or higher
class Student
{
#[Assert\AtLeastOneOf([
new Assert\Regex('/#/'),
new Assert\Length(min: 10),
])]
protected $plainPassword;
#[Assert\AtLeastOneOf([
new Assert\Count(min: 3),
new Assert\All(
new Assert\GreaterThanOrEqual(5)
),
])]
protected $grades;
}
|
Options ¶
constraints ¶
type: array
[default option]
This required option is the array of validation constraints from which at least one of has to be satisfied in order for the validation to succeed.
includeInternalMessages ¶
type: boolean
default: true
If set to true
, the message that is shown if the validation fails,
will include the list of messages for the internal constraints. See option
message for an example.
message ¶
type: string
default: This value should satisfy at least one of the following constraints:
This is the intro of the message that will be shown if the validation fails. By default,
it will be followed by the list of messages for the internal constraints
(configurable by includeInternalMessages option) . For example,
if the above grades
property fails to validate, the message will be
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 ¶
type: string
default: Each element of this collection should satisfy its own set of constraints.
This is the message that will be shown if the validation fails and the internal constraint is either All or Collection. See option message for an example.
groups
¶
type: array
| string
It defines the validation group or groups of this constraint. Read more about validation groups.
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.