Compound

To the contrary to the other constraints, this constraint cannot be used on its own. Instead, it allows you to create your own set of reusable constraints, representing rules to use consistently across your application, by extending the constraint.

他の制約とは対照的に、この制約は単独では使用できません。代わりに、制約を拡張することにより、アプリケーション全体で一貫して使用するルールを表す、再利用可能な制約の独自のセットを作成できます。
Applies to class or property or method
Class Compound
Validator CompoundValidator

Basic Usage

Suppose that you have different places where a user password must be validated, you can create your own named set or requirements to be reused consistently everywhere:

ユーザーパスワードを検証する必要があるさまざまな場所があるとします。独自の名前付きセットまたは要件を作成して、どこでも一貫して再利用できます。
  • Attributes
    属性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// src/Validator/Constraints/PasswordRequirements.php
namespace App\Validator\Constraints;

use Symfony\Component\Validator\Constraints\Compound;
use Symfony\Component\Validator\Constraints as Assert;

#[\Attribute]
class PasswordRequirements extends Compound
{
    protected function getConstraints(array $options): array
    {
        return [
            new Assert\NotBlank(),
            new Assert\Type('string'),
            new Assert\Length(['min' => 12]),
            new Assert\NotCompromisedPassword(),
        ];
    }
}

Add #[\Attribute] to the constraint class if you want to use it as an attribute in other classes. If the constraint has configuration options, define them as public properties on the constraint class.

他のクラスで属性として使用する場合は、制約クラスに #[\Attribute] を追加します。制約に構成オプションがある場合は、それらを制約クラスのパブリック プロパティとして定義します。

You can now use it anywhere you need it:

必要な場所で使用できるようになりました。
  • Attributes
    属性
  • YAML
    YAML
  • XML
    XML
  • PHP
    PHP
1
2
3
4
5
6
7
8
9
10
// src/Entity/User.php
namespace App\Entity\User;

use App\Validator\Constraints as Assert;

class User
{
    #[Assert\PasswordRequirements]
    public $plainPassword;
}

Options

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.

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