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.

この制約は、値が指定された制約の少なくとも 1 つを満たすことを確認します。検証は、1 つの制約が満たされるとすぐに停止します。

Basic Usage

The following constraints ensure that:

次の制約により、次のことが保証されます。
  • the password of a Student either contains # or is at least 10 characters long;
    生徒のパスワードに # が含まれているか、少なくとも 10 文字の長さです。
  • the grades of a Student is an array which contains at least 3 elements or that each element is greater than or equal to 5.
    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.

この必須オプションは、検証が成功するために少なくとも 1 つが満たされなければならない検証制約の配列です。

includeInternalMessages

type: boolean default: true

タイプ: ブール デフォルト: 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.

true に設定すると、検証が失敗した場合に表示されるメッセージに、内部制約に関するメッセージのリストが含まれます。例については、optionmessage を参照してください。

message

type: string default: This value should satisfy at least one of the following constraints:

タイプ: 文字列 デフォルト: この値は、次の制約の少なくとも 1 つを満たす必要があります。

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.

これは、検証が失敗した場合に表示されるメッセージのイントロです。デフォルトでは、内部制約 (includeInternalMessages オプションで構成可能) のメッセージのリストが続きます。たとえば、上記の成績プロパティが検証に失敗した場合、メッセージは次のようになります。この値は、次の制約の少なくとも 1 つを満たす必要があります:[1] このコレクションには 3 つ以上の要素が含まれている必要があります。[2]このコレクションの各要素は、独自の一連の制約を満たす必要があります。

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.

これは、検証が失敗し、内部制約が All または Collection のいずれかである場合に表示されるメッセージです。例については、オプション メッセージを参照してください。

groups

type: array | string

タイプ: 配列 |ストリング

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

この制約の検証グループを定義します。検証グループの詳細を参照してください。

payload

type: mixed default: null

タイプ: 混合 デフォルト: 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.

このオプションは、任意のドメイン固有のデータを制約に添付するために使用できます。構成されたペイロードは Validator コンポーネントによって使用されませんが、その処理は完全にユーザー次第です。

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.

たとえば、いくつかのエラー レベルを使用して、エラーの重大度に応じて、失敗した制約をフロントエンドで異なる方法で提示することができます。