Count

Validates that a given collection's (i.e. an array or an object that implements Countable) element count is between some minimum and maximum value.

特定のコレクション (つまり、Countable を実装する配列またはオブジェクト) の要素数が最小値と最大値の間にあることを検証します。
Applies to property or method
Class Count
Validator CountValidator

Basic Usage

To verify that the emails array field contains between 1 and 5 elements you might add the following:

email 配列フィールドに 1 ~ 5 個の要素が含まれていることを確認するには、次を追加します。
  • Attributes
    属性
  • YAML
    YAML
  • XML
    XML
  • PHP
    PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// src/Entity/Participant.php
namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Participant
{
    #[Assert\Count(
        min: 1,
        max: 5,
        minMessage: 'You must specify at least one email',
        maxMessage: 'You cannot specify more than {{ limit }} emails',
    )]
    protected $emails = [];
}

Options

divisibleBy

type: integer default: null

タイプ: 整数 デフォルト: null

Validates that the number of elements of the given collection is divisible by a certain number.

指定されたコレクションの要素数が特定の数で割り切れることを検証します。

See also

こちらもご覧ください

If you need to validate that other types of data different from collections are divisible by a certain number, use the DivisibleBy constraint.

コレクションとは異なる他のタイプのデータが特定の数で割り切れることを検証する必要がある場合は、DivisibleBy 制約を使用します。

divisibleByMessage

type: string default: The number of elements in this collection should be a multiple of {{ compared_value }}.

タイプ: 文字列 デフォルト: このコレクション内の要素の数は、{{ 比較値 }} の倍数である必要があります。

The message that will be shown if the number of elements of the given collection is not divisible by the number defined in the divisibleBy option.

指定されたコレクションの要素数が divisibleBy オプションで定義された数で割り切れない場合に表示されるメッセージ。

You can use the following parameters in this message:

このメッセージでは、次のパラメーターを使用できます。
Parameter Description
{{ compared_value }} The number configured in the divisibleBy option

exactMessage

type: string default: This collection should contain exactly {{ limit }} elements.

タイプ: 文字列 デフォルト: このコレクションには、正確に {{ limit }} 要素が含まれている必要があります。

The message that will be shown if min and max values are equal and the underlying collection elements count is not exactly this value.

最小値と最大値が等しく、基になるコレクション要素の数が正確にこの値でない場合に表示されるメッセージ。

You can use the following parameters in this message:

このメッセージでは、次のパラメーターを使用できます。
Parameter Description
{{ count }} The current collection size
{{ limit }} The exact expected collection size

groups

type: array | string

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

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

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

max

type: integer

タイプ: 整数

This option is the "max" count value. Validation will fail if the given collection elements count is greater than this max value.

このオプションは「最大」カウント値です。指定されたコレクション要素数がこの最大値より大きい場合、検証は失敗します。

This option is required when the min option is not defined.

このオプションは、min オプションが定義されていない場合に必要です。

maxMessage

type: string default: This collection should contain {{ limit }} elements or less.

タイプ: 文字列 デフォルト: このコレクションには {{ limit }} 個以下の要素が含まれている必要があります。

The message that will be shown if the underlying collection elements count is more than the max option.

基になるコレクション要素数が max オプションを超えた場合に表示されるメッセージ。

You can use the following parameters in this message:

このメッセージでは、次のパラメーターを使用できます。
Parameter Description
{{ count }} The current collection size
{{ limit }} The upper limit

min

type: integer

タイプ: 整数

This option is the "min" count value. Validation will fail if the given collection elements count is less than this min value.

このオプションは「最小」カウント値です。指定されたコレクション要素数がこの最小値より少ない場合、検証は失敗します。

This option is required when the max option is not defined.

このオプションは、max オプションが定義されていない場合に必要です。

minMessage

type: string default: This collection should contain {{ limit }} elements or more.

タイプ: 文字列 デフォルト: このコレクションには {{ limit }} 個以上の要素が含まれている必要があります。

The message that will be shown if the underlying collection elements count is less than the min option.

基になるコレクション要素数が min オプションより少ない場合に表示されるメッセージ。

You can use the following parameters in this message:

このメッセージでは、次のパラメーターを使用できます。
Parameter Description
{{ count }} The current collection size
{{ limit }} The lower limit

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.

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