CurrencyType Field

The CurrencyType is a subset of the ChoiceType that allows the user to select from a large list of 3-letter ISO 4217 currencies.

CurrencyType は、ユーザーが 3 文字の ISO 4217 通貨の大きなリストから選択できるようにする ChoiceType のサブセットです。

Unlike the ChoiceType, you don't need to specify a choices option as the field type automatically uses a large list of currencies. You can specify the option manually, but then you should just use the ChoiceType directly.

ChoiceType とは異なり、フィールド タイプは通貨の大きなリストを自動的に使用するため、choices オプションを指定する必要はありません。オプションは手動で指定できますが、ChoiceType を直接使用する必要があります。
Rendered as can be various tags (see ChoiceType Field (select drop-downs, radio buttons & checkboxes))
Default invalid message Please select a valid currency.
Legacy invalid message The value {{ value }} is not valid.
Parent type ChoiceType
Class CurrencyType

Tip

ヒント

The full list of options defined and inherited by this form type is available running this command in your app:

このフォーム タイプによって定義および継承されるオプションの完全なリストは、アプリで次のコマンドを実行して利用できます。
1
2
# replace 'FooType' by the class name of your form type
$ php bin/console debug:form FooType

Field Options

choice_translation_locale

type: string or null default: null

タイプ: 文字列または null デフォルト: null

This option determines if the choice values should be translated into a different locale than the current one.

このオプションは、選択値を現在のものとは異なるロケールに翻訳する必要があるかどうかを決定します。

The values of the choice_translation_locale option can be null (reuse the current translation locale) or a string which represents the exact translation locale to use.

choice_translation_locale オプションの値は、null (現在の翻訳ロケールを再利用) または使用する正確な翻訳ロケールを表す文字列にすることができます。

Overridden Options

choices

default: Symfony\Component\Intl\Currencies::getNames()

デフォルト: Symfony\Component\Intl\Currencies::getNames()

The choices option defaults to all currencies.

選択オプションのデフォルトはすべての通貨です。

Caution

注意

If you want to override the built-in choices of the currency type, you will also have to set the choice_loader option to null.

組み込みの通貨タイプの選択肢をオーバーライドする場合は、choice_loader オプションも null に設定する必要があります。

choice_translation_domain

type: string, boolean or null default: false

タイプ: 文字列、ブール値、または null デフォルト: false

This option determines if the choice values should be translated and in which translation domain.

このオプションは、選択値を翻訳するかどうか、およびどの翻訳ドメインで翻訳するかを決定します。

The values of the choice_translation_domain option can be true (reuse the current translation domain), false (disable translation), null (uses the parent translation domain or the default domain) or a string which represents the exact translation domain to use.

choice_translation_domain オプションの値は、true (現在の翻訳ドメインを再利用する)、false (翻訳を無効にする)、null (親の翻訳ドメインまたはデフォルト ドメインを使用する)、または使用する正確な翻訳ドメインを表す文字列です。

invalid_message

type: string default: This value is not valid

タイプ: 文字列 デフォルト: この値は無効です

This is the validation error message that's used if the data entered into this field doesn't make sense (i.e. fails validation).

これは、このフィールドに入力されたデータが意味をなさない場合 (つまり、検証に失敗した場合) に使用される検証エラー メッセージです。

This might happen, for example, if the user enters a nonsense string into a TimeType field that cannot be converted into a real time or if the user enters a string (e.g. apple) into a number field.

これは、たとえば、ユーザーがリアルタイムに変換できない無意味な文字列を TimeType フィールドに入力した場合、またはユーザーが文字列 (例: apple) を数値フィールドに入力した場合に発生する可能性があります。

Normal (business logic) validation (such as when setting a minimum length for a field) should be set using validation messages with your validation rules (reference).

通常の (ビジネス ロジック) 検証 (フィールドの最小長を設定する場合など) は、validationrules (参照) で検証メッセージを使用して設定する必要があります。

Inherited Options

These options inherit from the ChoiceType:

これらのオプションは ChoiceType から継承します。

error_bubbling

type: boolean default: false unless the form is compound

タイプ: ブール値 デフォルト: フォームが複合でない限り false

If true, any errors for this field will be passed to the parent field or form. For example, if set to true on a normal field, any errors for that field will be attached to the main form, not to the specific field.

true の場合、このフィールドのエラーは親フィールドまたはフォームに渡されます。たとえば、通常のフィールドで true に設定すると、そのフィールドのエラーは特定のフィールドではなく、メイン フォームに添付されます。

expanded

type: boolean default: false

タイプ: ブール デフォルト: false

If set to true, radio buttons or checkboxes will be rendered (depending on the multiple value). If false, a select element will be rendered.

true に設定すると、ラジオ ボタンまたはチェックボックスが表示されます (複数の値に応じて)。 false の場合、select 要素がレンダリングされます。

multiple

type: boolean default: false

タイプ: ブール デフォルト: false

If true, the user will be able to select multiple options (as opposed to choosing just one option). Depending on the value of the expanded option, this will render either a select tag or checkboxes if true and a select tag or radio buttons if false. The returned value will be an array.

true の場合、ユーザーは (1 つのオプションのみを選択するのではなく) 複数のオプションを選択できます。 Expandedoption の値に応じて、true の場合は選択タグまたはチェックボックスがレンダリングされ、false の場合は選択タグまたはラジオ ボタンがレンダリングされます。戻り値は配列になります。

placeholder

type: string or boolean

タイプ: 文字列またはブール値

This option determines whether or not a special "empty" option (e.g. "Choose an option") will appear at the top of a select widget. This option only applies if the multiple option is set to false.

このオプションは、特別な「空の」オプション (「オプションの選択」など) を選択ウィジェットの上部に表示するかどうかを決定します。このオプションは、multiple オプションが false に設定されている場合にのみ適用されます。
  • Add an empty value with "Choose an option" as the text:

    「オプションを選択してください」という空の値をテキストとして追加します。
    1
    2
    3
    4
    5
    6
    use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
    // ...
    
    $builder->add('states', ChoiceType::class, [
        'placeholder' => 'Choose an option',
    ]);
  • Guarantee that no "empty" value option is displayed:

    「空の」値オプションが表示されないことを保証します。
    1
    2
    3
    4
    5
    6
    use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
    // ...
    
    $builder->add('states', ChoiceType::class, [
        'placeholder' => false,
    ]);

If you leave the placeholder option unset, then a blank (with no text) option will automatically be added if and only if the required option is false:

プレースホルダー オプションを未設定のままにすると、必要なオプションが false の場合にのみ、空白 (テキストなし) のオプションが自動的に追加されます。
1
2
3
4
5
6
7
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
// ...

// a blank (with no text) option will be added
$builder->add('states', ChoiceType::class, [
    'required' => false,
]);

preferred_choices

type: array, callable, string or PropertyPath default: []

タイプ: 配列、呼び出し可能、文字列または PropertyPath デフォルト: []

This option allows you to display certain choices at the top of your list with a visual separator between them and the complete list of options. If you have a form of languages, you can list the most popular on top, like Bork and Pirate:

このオプションを使用すると、リストの上部に特定の選択肢を表示し、選択肢とオプションの完全なリストを視覚的に区切ることができます。言語の形式がある場合は、Bork や Pirate など、最も人気のあるものを一番上にリストできます。
1
2
3
4
5
6
7
8
9
10
11
12
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
// ...

$builder->add('language', ChoiceType::class, [
    'choices' => [
        'English' => 'en',
        'Spanish' => 'es',
        'Bork' => 'muppets',
        'Pirate' => 'arr',
    ],
    'preferred_choices' => ['muppets', 'arr'],
]);

This options can also be a callback function to give you more flexibility. This might be especially useful if your values are objects:

このオプションは、柔軟性を高めるためにコールバック関数にすることもできます。これは、値がオブジェクトである場合に特に役立ちます。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
// ...

$builder->add('publishAt', ChoiceType::class, [
    'choices' => [
        'now' => new \DateTime('now'),
        'tomorrow' => new \DateTime('+1 day'),
        '1 week' => new \DateTime('+1 week'),
        '1 month' => new \DateTime('+1 month'),
    ],
    'preferred_choices' => function ($choice, $key, $value) {
        // prefer options within 3 days
        return $choice <= new \DateTime('+3 days');
    },
]);

This will "prefer" the "now" and "tomorrow" choices only:

これは、「今」と「明日」の選択肢のみを「優先」します。

Finally, if your values are objects, you can also specify a property path string on the object that will return true or false.

最後に、値がオブジェクトの場合、true または false を返すオブジェクトのプロパティ パス文字列を指定することもできます。

The preferred choices are only meaningful when rendering a select element (i.e. expanded false). The preferred choices and normal choices are separated visually by a set of dotted lines (i.e. -------------------). This can be customized when rendering the field:

優先される選択肢は、select 要素をレンダリングする場合 (つまり、expanded false) にのみ意味があります。好ましい選択肢と通常の選​​択肢は、一連の点線 (つまり -------------------) によって視覚的に区切られています。これは、フィールドをレンダリングするときにカスタマイズできます。
  • Twig
    小枝
  • PHP
    PHP
1
{{ form_widget(form.publishAt, { 'separator': '=====' }) }}

Tip

When defining a custom type, you should use the ChoiceList class helper:

カスタム タイプを定義するときは、ChoiceList クラス ヘルパーを使用する必要があります。
1
2
3
4
5
6
use Symfony\Component\Form\ChoiceList\ChoiceList;

// ...
$builder->add('choices', ChoiceType::class, [
    'preferred_choices' => ChoiceList::preferred($this, 'taggedAsFavorite'),
]);

See the "choice_loader" option documentation.

「choice_loader」オプションのドキュメントを参照してください。

trim

type: boolean default: false

タイプ: ブール デフォルト: false

Trimming is disabled by default because the selected value or values must match the given choice values exactly (and they could contain whitespaces).

選択された値または値が指定された選択肢の値と正確に一致する必要があるため (空白が含まれる可能性があるため)、トリミングはデフォルトで無効になっています。

These options inherit from the FormType:

これらのオプションは FormType から継承します。

attr

type: array default: []

タイプ: 配列 デフォルト: []

If you want to add extra attributes to an HTML field representation you can use the attr option. It's an associative array with HTML attributes as keys. This can be useful when you need to set a custom class for some widget:

HTML フィールド表現に追加の属性を追加する場合は、attr オプションを使用できます。これは、HTML 属性をキーとする連想配列です。これは、一部のウィジェットにカスタム クラスを設定する必要がある場合に役立ちます。
1
2
3
$builder->add('body', TextareaType::class, [
    'attr' => ['class' => 'tinymce'],
]);

See also

こちらもご覧ください

Use the row_attr option if you want to add these attributes to the form type row element.

これらの属性をフォーム タイプの行要素に追加する場合は、row_attr オプションを使用します。

data

type: mixed default: Defaults to field of the underlying structure.

タイプ: 混合 デフォルト: 基礎となる構造のフィールドにデフォルト設定されます。

When you create a form, each field initially displays the value of the corresponding property of the form's domain data (e.g. if you bind an object to the form). If you want to override this initial value for the form or an individual field, you can set it in the data option:

フォームを作成すると、最初に各フィールドに、フォームのドメイン データの対応するプロパティの値が表示されます (たとえば、オブジェクトをフォームにバインドした場合)。フォームまたは個々のフィールドのこの初期値をオーバーライドする場合は、データ オプションで設定できます。
1
2
3
4
5
6
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
// ...

$builder->add('token', HiddenType::class, [
    'data' => 'abcdef',
]);

Caution

注意

The data option always overrides the value taken from the domain data (object) when rendering. This means the object value is also overridden when the form edits an already persisted object, causing it to lose its persisted value when the form is submitted.

data オプションは、レンダリング時にドメイン データ (オブジェクト) から取得した値を常にオーバーライドします。これは、フォームがすでに永続化されているオブジェクトを編集すると、オブジェクトの値も上書きされ、フォームが送信されると永続化された値が失われることを意味します。

disabled

type: boolean default: false

タイプ: ブール値デフォルト: false

If you don't want a user to modify the value of a field, you can set the disabled option to true. Any submitted value will be ignored.

ユーザーがフィールドの値を変更できないようにするには、disabled オプションを true に設定します。送信された値は無視されます。

empty_data

type: mixed

タイプ: 混合

The actual default value of this option depends on other field options:

このオプションの実際のデフォルト値は、他のフィールド オプションによって異なります。
  • If multiple is false and expanded is false, then '' (empty string);
    multiple が false で、expanded が false の場合、''(空の文字列);
  • Otherwise [] (empty array).
    それ以外の場合は [] (空の配列)。

This option determines what value the field will return when the submitted value is empty (or missing). It does not set an initial value if none is provided when the form is rendered in a view.

このオプションは、送信された値が空 (または欠落) の場合にフィールドが返す値を決定します。フォームがビューにレンダリングされるときに何も指定されていない場合、初期値は設定されません。

This means it helps you handling form submission with blank fields. For example, if you want the name field to be explicitly set to John Doe when no value is selected, you can do it like this:

これは、空白のフィールドでフォーム送信を処理するのに役立つことを意味します。たとえば、値が選択されていないときに名前フィールドを明示的に John Doe に設定する場合は、次のようにします。
1
2
3
4
$builder->add('name', null, [
    'required'   => false,
    'empty_data' => 'John Doe',
]);

This will still render an empty text box, but upon submission the John Doe value will be set. Use the data or placeholder options to show this initial value in the rendered form.

これでも空のテキスト ボックスが表示されますが、送信時に John Doevalue が設定されます。データまたはプレースホルダー オプションを使用して、レンダリングされたフォームでこの初期値を表示します。

If a form is compound, you can set empty_data as an array, object or closure. See the How to Configure empty Data for a Form Class article for more details about these options.

フォームが複合の場合、empty_data を配列、オブジェクト、またはクロージャーとして設定できます。これらのオプションの詳細については、フォーム クラスの空のデータを構成する方法の記事を参照してください。

Note

ノート

If you want to set the empty_data option for your entire form class, see the How to Configure empty Data for a Form Class article.

フォーム クラス全体に empty_data オプションを設定する場合は、フォーム クラスの空のデータを構成する方法の記事を参照してください。

Caution

注意

Form data transformers will still be applied to the empty_data value. This means that an empty string will be cast to null. Use a custom data transformer if you explicitly want to return the empty string.

フォーム データ トランスフォーマーは引き続き empty_data 値に適用されます。これは、空の文字列が null にキャストされることを意味します。空の文字列を明示的に返したい場合は、カスタム データ トランスフォーマーを使用します。

help

type: string or TranslatableInterface default: null

タイプ: 文字列または TranslatableInterface デフォルト: null

Allows you to define a help message for the form field, which by default is rendered below the field:

フォーム フィールドのヘルプ メッセージを定義できます。デフォルトではフィールドの下に表示されます。
1
2
3
4
5
6
7
8
9
10
11
12
13
use Symfony\Component\Translation\TranslatableMessage;

$builder
    ->add('zipCode', null, [
        'help' => 'The ZIP/Postal code for your credit card\'s billing address.',
    ])

    // ...

    ->add('status', null, [
        'help' => new TranslatableMessage('order.status', ['%order_id%' => $order->getId()], 'store'),
    ])
;

6.2

6.2

The support for TranslatableInterface objects as help contents was introduced in Symfony 6.2.

ヘルプ コンテンツとしての TranslatableInterface オブジェクトのサポートは、Symfony 6.2 で導入されました。

help_attr

type: array default: []

タイプ: 配列 デフォルト: []

Sets the HTML attributes for the element used to display the help message of the form field. Its value is an associative array with HTML attribute names as keys. These attributes can also be set in the template:

フォーム フィールドのヘルプ メッセージを表示するために使用される要素の HTML 属性を設定します。その値は、HTML 属性名をキーとする連想配列です。これらの属性は、テンプレートで設定することもできます。
1
2
3
{{ form_help(form.name, 'Your name', {
    'help_attr': {'class': 'CUSTOM_LABEL_CLASS'}
}) }}

help_html

type: boolean default: false

タイプ: ブール デフォルト: false

By default, the contents of the help option are escaped before rendering them in the template. Set this option to true to not escape them, which is useful when the help contains HTML elements.

デフォルトでは、ヘルプ オプションの内容は、テンプレートでレンダリングする前にエスケープされます。エスケープしないようにするには、このオプションを true に設定します。これは、ヘルプに HTML 要素が含まれている場合に役立ちます。

label

type: string or TranslatableMessage default: The label is "guessed" from the field name

タイプ: 文字列または TranslatableMessage デフォルト: ラベルはフィールド名から「推測」されます

Sets the label that will be used when rendering the field. Setting to false will suppress the label:

フィールドのレンダリング時に使用されるラベルを設定します。 false に設定すると、ラベルが抑制されます。
1
2
3
4
5
6
7
8
use Symfony\Component\Translation\TranslatableMessage;

$builder
    ->add('zipCode', null, [
        'label' => 'The ZIP/Postal code',
        // optionally, you can use TranslatableMessage objects as the label content
        'label' => new TranslatableMessage('address.zipCode', ['%country%' => $country], 'address'),
    ])

The label can also be set in the template:

ラベルはテンプレートで設定することもできます:
  • Twig
    小枝
  • PHP
    PHP
1
{{ form_label(form.name, 'Your name') }}

label_attr

type: array default: []

タイプ: 配列 デフォルト: []

Sets the HTML attributes for the <label> element, which will be used when rendering the label for the field. It's an associative array with HTML attribute as a key. This attributes can also be directly set inside the template:

フィールドのラベルをレンダリングするときに使用される要素の HTML 属性を設定します。 HTML属性をキーにした連想配列です。この属性は、テンプレート内で直接設定することもできます:
  • Twig
    小枝
  • PHP
    PHP
1
2
3
{{ form_label(form.name, 'Your name', {
    'label_attr': {'class': 'CUSTOM_LABEL_CLASS'}
}) }}

label_format

type: string default: null

タイプ: 文字列 デフォルト: null

Configures the string used as the label of the field, in case the label option was not set. This is useful when using keyword translation messages.

label オプションが設定されていない場合に、フィールドのラベルとして使用される文字列を構成します。これは、キーワード翻訳メッセージを使用する場合に便利です。

If you're using keyword translation messages as labels, you often end up having multiple keyword messages for the same label (e.g. profile_address_street, invoice_address_street). This is because the label is built for each "path" to a field. To avoid duplicated keyword messages, you can configure the label format to a static value, like:

キーワード翻訳メッセージをラベルとして使用している場合、同じラベルに複数のキーワード メッセージが含まれることがよくあります (例: profile_address_street,invoice_address_street)。これは、フィールドへの「パス」ごとにラベルが作成されるためです。キーワード メッセージの重複を避けるために、次のように labelformat を静的な値に設定できます。
1
2
3
4
5
6
7
8
// ...
$profileFormBuilder->add('address', AddressType::class, [
    'label_format' => 'form.address.%name%',
]);

$invoiceFormBuilder->add('invoice', AddressType::class, [
    'label_format' => 'form.address.%name%',
]);

This option is inherited by the child types. With the code above, the label of the street field of both forms will use the form.address.street keyword message.

このオプションは子タイプに継承されます。上記のコードでは、両方のフォームのストリート フィールドのラベルが form.address.street キーワード メッセージを使用します。

Two variables are available in the label format:

ラベル形式では、次の 2 つの変数を使用できます。
%id%
A unique identifier for the field, consisting of the complete path to the field and the field name (e.g. profile_address_street);
フィールドへの完全なパスとフィールド名 (例: profile_address_street) で構成される、フィールドの一意の識別子。
%name%
The field name (e.g. street).
フィールド名 (通りなど)。

The default value (null) results in a "humanized" version of the field name.

デフォルト値 (null) は、フィールド名の「人間化された」バージョンになります。

Note

ノート

The label_format option is evaluated in the form theme. Make sure to update your templates in case you customized form theming.

label_format オプションは、フォーム テーマで評価されます。フォームのテーマをカスタマイズした場合は、必ずテンプレートを更新してください。

mapped

type: boolean default: true

タイプ: ブール値デフォルト: true

If you wish the field to be ignored when reading or writing to the object, you can set the mapped option to false.

オブジェクトの読み取りまたは書き込み時にフィールドを無視する場合は、マップされたオプションを false に設定できます。

required

type: boolean default: true

If true, an HTML5 required attribute will be rendered. The corresponding label will also render with a required class.

true の場合、HTML5 必須属性がレンダリングされます。対応するラベルも必要なクラスでレンダリングされます。

This is superficial and independent of validation. At best, if you let Symfony guess your field type, then the value of this option will be guessed from your validation information.

これは表面的なものであり、検証とは無関係です。せいぜい、Symfony にフィールド タイプを推測させれば、このオプションの値は検証情報から推測されます。

Note

ノート

The required option also affects how empty data for each field is handled. For more details, see the empty_data option.

必須オプションは、各フィールドの空のデータの処理方法にも影響します。詳細については、empty_data オプションを参照してください。

row_attr

type: array default: []

タイプ: 配列 デフォルト: []

An associative array of the HTML attributes added to the element which is used to render the form type row:

フォーム タイプの行をレンダリングするために使用される要素に追加される HTML 属性の連想配列:
1
2
3
$builder->add('body', TextareaType::class, [
    'row_attr' => ['class' => 'text-editor', 'id' => '...'],
]);

See also

こちらもご覧ください

Use the attr option if you want to add these attributes to the form type widget element.

これらの属性をフォーム タイプのウィジェット要素に追加する場合は、attr オプションを使用します。