FormType Field

The FormType predefines a couple of options that are then available on all types for which FormType is the parent.

FormType は、FormType が親であるすべてのタイプで使用できるいくつかのオプションを事前定義します。
Default invalid message This value is not valid.
Legacy invalid message This value is not valid.
Parent none
Class FormType

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

action

type: string default: empty string

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

This option specifies where to send the form's data on submission (usually a URI). Its value is rendered as the action attribute of the form element. An empty value is considered a same-document reference, i.e. the form will be submitted to the same URI that rendered the form.

このオプションは、送信時にフォームのデータをどこに送信するかを指定します (通常は URI)。その値は、フォーム要素のアクション属性としてレンダリングされます。空の値は、同じドキュメント参照と見なされます。つまり、フォームは、フォームをレンダリングしたのと同じ URI に送信されます。

allow_extra_fields

type: boolean default: false

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

Usually, if you submit extra fields that aren't configured in your form, you'll get a "This form should not contain extra fields." validation error.

通常、フォームで構成されていない追加のフィールドを送信すると、「このフォームには追加のフィールドを含めないでください」というメッセージが表示されます。検証エラー。

You can silence this validation error by enabling the allow_extra_fields option on the form.

フォームで allow_extra_fields オプションを有効にすることで、この検証エラーを黙らせることができます。

by_reference

type: boolean default: true

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

In most cases, if you have an author field, then you expect setAuthor() to be called on the underlying object. In some cases, however, setAuthor() may not be called. Setting by_reference to false ensures that the setter is called in all cases.

ほとんどの場合、author フィールドがある場合は、基礎となるオブジェクトで setAuthor() が呼び出されることを期待します。ただし、setAuthor() が呼び出されない場合もあります。 by_reference を false に設定すると、すべての場合にセッターが呼び出されます。

To explain this further, here's a simple example:

これをさらに説明するために、簡単な例を次に示します。
1
2
3
4
5
6
7
8
9
10
11
12
13
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
// ...

$builder = $this->createFormBuilder($article);
$builder
    ->add('title', TextType::class)
    ->add(
        $builder->create('author', FormType::class, ['by_reference' => ?])
            ->add('name', TextType::class)
            ->add('email', EmailType::class)
    )

If by_reference is true, the following takes place behind the scenes when you call submit() (or handleRequest()) on the form:

by_reference が true の場合、フォームで submit() (または handleRequest()) を呼び出すと、舞台裏で次のことが行われます。
1
2
3
$article->setTitle('...');
$article->getAuthor()->setName('...');
$article->getAuthor()->setEmail('...');

Notice that setAuthor() is not called. The author is modified by reference.

setAuthor() が呼び出されないことに注意してください。著者は参照によって変更されます。

If you set by_reference to false, submitting looks like this:

by_reference を false に設定すると、送信は次のようになります。
1
2
3
4
5
$article->setTitle('...');
$author = clone $article->getAuthor();
$author->setName('...');
$author->setEmail('...');
$article->setAuthor($author);

So, all that by_reference=false really does is that it clones the object, which enforces the framework to call the setter on the parent object.

そのため、by_reference=false が実際に行うことは、オブジェクトを複製することだけです。これにより、フレームワークは親オブジェクトのセッターを呼び出すようになります。

Similarly, if you're using the CollectionType field where your underlying collection data is an object (like with Doctrine's ArrayCollection), then by_reference must be set to false if you need the adder and remover (e.g. addAuthor() and removeAuthor()) to be called.

同様に、基になるコレクション データがオブジェクトである CollectionTypefield を使用している場合 (withDoctrine の ArrayCollection など)、adder と remover (たとえば、addAuthor() と removeAuthor()) を呼び出す必要がある場合は、by_reference を false に設定する必要があります。

compound

type: boolean default: true

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

A compound form can be either an entire <form> element or a group of form fields (rendered for example inside a <div> or <tr> container elements). Compound forms use the DataMapperInterface to initialize their children or to write back their submitted data.

複合フォームは、要素全体またはフォーム フィールドのグループのいずれかです (たとえば、コンテナ要素内でレンダリングされます)。複合フォームは、DataMapperInterface を使用して、子を初期化したり、送信されたデータを書き戻したりします。

A simple (non-compound) form is rendered as any of these HTML elements: <input> (TextType, FileType, HiddenType), <textarea> (TextareaType) or <select> (ChoiceType).

単純な (複合ではない) フォームは、(TextType、FileType、HiddenType)、(TextareaType)、または (ChoiceType) のいずれかの HTML 要素としてレンダリングされます。

Some core types like date related types or the ChoiceType are simple or compound depending on other options (such as expanded or widget). They will either behave as a simple text field or as a group of text or choice fields.

日付関連のタイプや ChoiceType などの一部のコア タイプは、他のオプション (展開またはウィジェットなど) に応じて単純または複合になります。これらは、単純なテキスト フィールドとして、またはテキストまたは選択フィールドのグループとして動作します。

constraints

type: array or Constraint default: null

タイプ: 配列または制約のデフォルト: null

Allows you to attach one or more validation constraints to a specific field. For more information, see Adding Validation. This option is added in the FormTypeValidatorExtension form extension.

特定のフィールドに 1 つ以上の検証制約を付加できます。詳細については、検証の追加を参照してください。このオプションは、FormTypeValidatorExtensionform 拡張機能に追加されています。

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

data_class

type: string

タイプ: 文字列

This option is used to set the appropriate data mapper to be used by the form, so you can use it for any form field type which requires an object:

このオプションは、フォームで使用される適切なデータ マッパーを設定するために使用されるため、オブジェクトを必要とする任意のフォーム フィールド タイプに使用できます。
1
2
3
4
5
6
7
use App\Entity\Media;
use App\Form\MediaType;
// ...

$builder->add('media', MediaType::class, [
    'data_class' => Media::class,
]);

empty_data

type: mixed

タイプ: 混合

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

このオプションの実際のデフォルト値は、他のフィールド オプションによって異なります。
  • If data_class is set and required is true, then new $data_class();
    data_class が設定されていて、required が true の場合、new $data_class();
  • If data_class is set and required is false, then null;
    data_class が設定されていて、required が false の場合は null。
  • If data_class is not set and compound is true, then [] (empty array);
    data_class が設定されておらず、compound が true の場合、[](空の配列);
  • If data_class is not set and compound is false, then '' (empty string).
    data_class が設定されておらず、compound が false の場合、''(空の文字列)。

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 にキャストされることを意味します。空の文字列を明示的に返したい場合は、カスタム データ トランスフォーマーを使用します。

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 に設定すると、そのフィールドのエラーは特定のフィールドではなく、メイン フォームに添付されます。

error_mapping

type: array default: []

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

This option allows you to modify the target of a validation error.

このオプションを使用すると、検証エラーのターゲットを変更できます。

Imagine you have a custom method named matchingCityAndZipCode() that validates whether the city and zip code match. Unfortunately, there is no matchingCityAndZipCode field in your form, so all that Symfony can do is display the error on top of the form.

都市と郵便番号が一致するかどうかを検証する matchingCityAndZipCode() という名前のカスタム メソッドがあるとします。残念ながら、あなたのフォームには一致する CityAndZipCode フィールドがないため、Symfony ができることはフォームの上にエラーを表示することだけです。

With customized error mapping, you can do better: map the error to the city field so that it displays above it:

カスタマイズされたエラー マッピングを使用すると、より適切に実行できます。エラーを cityfield にマッピングして、その上に表示されるようにします。
1
2
3
4
5
6
7
8
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'error_mapping' => [
            'matchingCityAndZipCode' => 'city',
        ],
    ]);
}

Here are the rules for the left and the right side of the mapping:

マッピングの左側と右側の規則は次のとおりです。
  • The left side contains property paths;
    左側にはプロパティ パスが含まれています。
  • If the violation is generated on a property or method of a class, its path is the propertyName;
    違反がクラスのプロパティまたはメソッドで生成された場合、そのパスは propertyName です。
  • If the violation is generated on an entry of an array or ArrayAccess object, the property path is [indexName];
    違反が配列または ArrayAccess オブジェクトのエントリで生成された場合、プロパティ パスは [indexName] です。
  • You can construct nested property paths by concatenating them, separating properties by dots. For example: addresses[work].matchingCityAndZipCode;
    プロパティをドットで区切って連結することにより、ネストされたプロパティ パスを作成できます。例: address[work].matchingCityAndZipCode;
  • The right side contains the names of fields in the form.
    右側には、フォーム内のフィールドの名前が含まれています。

By default, errors for any property that is not mapped will bubble up to the parent form. You can use the dot (.) on the left side to map errors of all unmapped properties to a particular field. For instance, to map all these errors to the city field, use:

デフォルトでは、マップされていないプロパティのエラーは親フォームにバブル アップします。左側のドット (.) を使用して、マップされていないすべてのプロパティのエラーを特定のフィールドにマップできます。たとえば、これらすべてのエラーを都市フィールドにマップするには、次を使用します。
1
2
3
4
5
$resolver->setDefaults([
    'error_mapping' => [
        '.' => 'city',
    ],
]);

extra_fields_message

type: string default: This form should not contain extra fields.

タイプ: 文字列 デフォルト: このフォームには余分なフィールドを含めないでください。

This is the validation error message that's used if the submitted form data contains one or more fields that are not part of the form definition. The placeholder {{ extra_fields }} can be used to display a comma separated list of the submitted extra field names.

これは、送信されたフォーム データにフォーム定義の一部ではない 1 つ以上のフィールドが含まれている場合に使用される検証エラー メッセージです。プレースホルダー {{ extra_fields }} を使用して、送信された追加フィールド名のコンマ区切りリストを表示できます。

This message can be pluralized, see formatting pluralized messages for details.

このメッセージは複数形にすることができます。詳細については、複数形のメッセージのフォーマットを参照してください。

form_attr

type: boolean or string default: false

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

When true and used on a form element, it adds a "form" attribute to its HTML field representation with its HTML form id. By doing this, a form element can be rendered outside the HTML form while still working as expected:

true をフォーム要素で使用すると、HTML フォーム ID を持つ HTML フィールド表現に「フォーム」属性が追加されます。これにより、期待どおりに機能しながら、フォーム要素を HTML フォームの外側にレンダリングできます。
1
2
3
$builder->add('body', TextareaType::class, [
    'form_attr' => true,
]);

This can be useful when you need to solve nested form problems. You can also set this to true on a root form to automatically set the "form" attribute on all its children.

これは、ネストされたフォームの問題を解決する必要がある場合に役立ちます。また、ルート フォームでこれを true に設定して、そのすべての子に「フォーム」属性を自動的に設定することもできます。

Note

ノート

When the root form has no ID, form_attr is required to be a string identifier to be used as the form ID.

ルート フォームに ID がない場合、form_attr はフォーム ID として使用される文字列識別子である必要があります。

getter

type: callable default: null

タイプ: 呼び出し可能 デフォルト: null

When provided, this callable will be invoked to read the value from the underlying object that will be used to populate the form field.

提供されると、この callable が呼び出されて、フォーム フィールドへの入力に使用される基になるオブジェクトから値が読み取られます。

More details are available in the section on When and How to Use Data Mappers.

詳細については、データ マッパーをいつ、どのように使用するかに関するセクションを参照してください。

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 要素が含まれている場合に役立ちます。

help_translation_parameters

type: array default: []

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

The content of the help option is translated before displaying it, so it can contain translation placeholders. This option defines the values used to replace those placeholders.

ヘルプ オプションのコンテンツは表示前に翻訳されるため、翻訳プレースホルダーを含めることができます。このオプションは、これらのプレースホルダーを置き換えるために使用される値を定義します。

Given this translation message:

この翻訳メッセージを考えると:
1
2
# translations/messages.en.yaml
form.order.id.help: 'This will be the reference in communications with %company%'

You can specify the placeholder values as follows:

プレースホルダー値は次のように指定できます。
1
2
3
4
5
6
$builder->add('id', null, [
    'help' => 'form.order.id.help',
    'help_translation_parameters' => [
        '%company%' => 'ACME Inc.',
    ],
]);

The help_translation_parameters option of children fields is merged with the same option of their parents, so children can reuse and/or override any of the parent placeholders.

子フィールドの help_translation_parameters オプションは、親の同じオプションとマージされるため、子は親のプレースホルダーを再利用および/またはオーバーライドできます。

inherit_data

type: boolean default: false

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

This option determines if the form will inherit data from its parent form. This can be useful if you have a set of fields that are duplicated across multiple forms. See How to Reduce Code Duplication with "inherit_data".

このオプションは、フォームが親フォームからデータを継承するかどうかを決定します。これは、複数のフォームで重複する一連のフィールドがある場合に役立ちます。 「inherit_data」でコードの重複を減らす方法を参照してください。

Caution

注意

When a field has the inherit_data option set, it uses the data of the parent form as is. This means that Data Transformers won't be applied to that field.

フィールドに inherit_data オプションが設定されている場合、親フォームのデータがそのまま使用されます。これは、Data Transformers がそのフィールドに適用されないことを意味します。

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 (参照) で検証メッセージを使用して設定する必要があります。

invalid_message_parameters

type: array default: []

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

When setting the invalid_message option, you may need to include some variables in the string. This can be done by adding placeholders to that option and including the variables in this option:

invalid_message オプションを設定する場合、文字列にいくつかの変数を含める必要がある場合があります。これは、そのオプションにプレースホルダーを追加し、このオプションに変数を含めることで実行できます。
1
2
3
4
5
$builder->add('someField', SomeFormType::class, [
    // ...
    'invalid_message' => 'You entered an invalid value, it should include %num% letters',
    'invalid_message_parameters' => ['%num%' => 6],
]);

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 に設定できます。

method

type: string default: POST

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

This option specifies the HTTP method used to submit the form's data. Its value is rendered as the method attribute of the form element and is used to decide whether to process the form submission in the handleRequest() method after submission. Possible values are:

このオプションは、フォームのデータを送信するために使用される HTTP メソッドを指定します。その値は、フォーム要素の method 属性としてレンダリングされ、送信後に handleRequest() メソッドでフォーム送信を処理するかどうかを決定するために使用されます。可能な値は次のとおりです。
  • POST
    役職
  • GET
    得る
  • PUT
    置く
  • DELETE
    消去
  • PATCH
    パッチ

Note

ノート

When the method is PUT, PATCH, or DELETE, Symfony will automatically render a _method hidden field in your form. This is used to "fake" these HTTP methods, as they're not supported on standard browsers. This can be useful when matching routes by HTTP method.

メソッドが PUT、PATCH、または DELETE の場合、Symfony はフォームに _method 隠しフィールドを自動的にレンダリングします。これらの HTTP メソッドは標準のブラウザではサポートされていないため、これらの HTTP メソッドを「偽装」するために使用されます。これは、HTTP メソッドでルートを照合する場合に役立ちます。

Note

ノート

The PATCH method allows submitting partial data. In other words, if the submitted form data is missing certain fields, those will be ignored and the default values (if any) will be used. With all other HTTP methods, if the submitted form data is missing some fields, those fields are set to null.

PATCH メソッドを使用すると、部分的なデータを送信できます。つまり、送信されたフォーム データに特定のフィールドが欠落している場合、それらは無視され、デフォルト値 (存在する場合) が使用されます。他のすべての HTTP メソッドでは、送信されたフォーム データに欠落しているフィールドがある場合、それらのフィールドは null に設定されます。

post_max_size_message

type: string default: The uploaded file was too large. Please try to upload a smaller file.

タイプ: 文字列 デフォルト: アップロードされたファイルが大きすぎます。小さいファイルをアップロードしてみてください。

This is the validation error message that's used if submitted POST form data exceeds php.ini's post_max_size directive. The {{ max }} placeholder can be used to display the allowed size.

これは、送信された POST フォームデータが php.ini の post_max_size ディレクティブを超えた場合に使用される検証エラー メッセージです。 {{ max }} プレースホルダーを使用して、許可されたサイズを表示できます。

Note

ノート

Validating the post_max_size only happens on the root form.

post_max_size の検証は、ルート フォームでのみ行われます。

property_path

type: PropertyPathInterface|string|null default: null

タイプ: PropertyPathInterface|string|null デフォルト: null

By default (when the value of this option is null) form fields read from and write to the properties with the same names in the form's domain object. The property_path option lets you define which property a field reads from and writes to. The value of this option can be any valid PropertyAccess syntax.

デフォルトでは (このオプションの値が null の場合)、フォーム フィールドは、フォームのドメイン オブジェクト内の同じ名前を持つプロパティから読み書きされます。 property_path オプションを使用すると、フィールドが読み書きするプロパティを定義できます。このオプションの値には、任意の有効な PropertyAccess 構文を指定できます。

required

type: boolean default: true

タイプ: ブール デフォルト: 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 オプションを参照してください。

setter

type: callable default: null

タイプ: 呼び出し可能 デフォルト: null

When provided, this callable will be invoked to map the form value back to the underlying object.

提供されると、この呼び出し可能オブジェクトが呼び出されて、フォームの値が基になるオブジェクトにマップされます。

More details are available in the section on When and How to Use Data Mappers.

詳細については、データ マッパーをいつ、どのように使用するかに関するセクションを参照してください。

trim

type: boolean default: true

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

If true, the whitespace of the submitted string value will be stripped via the trim function when the data is bound. This guarantees that if a value is submitted with extra whitespace, it will be removed before the value is merged back onto the underlying object.

true の場合、送信された文字列値の空白は、データがバインドされるときにトリム関数によって削除されます。これにより、余分な空白を含む値が送信された場合、値が基になるオブジェクトにマージされる前に削除されることが保証されます。

validation_groups

type: array, string, callable, GroupSequence or null default: null

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

This option is only valid on the root form and is used to specify which groups will be used by the validator.

このオプションは、ルート フォームでのみ有効で、バリデータが使用するグループを指定するために使用されます。

For null the validator will just use the Default group.

null の場合、バリデーターは Default グループのみを使用します。

If you specify the groups as an array or string they will be used by the validator as they are:

グループを配列または文字列として指定すると、それらはバリデーターによってそのまま使用されます。
1
2
3
4
5
6
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'validation_groups' => 'Registration',
    ]);
}

This is equivalent to passing the group as array:

これは、グループを配列として渡すことと同じです:
1
'validation_groups' => ['Registration'],

The form's data will be validated against all given groups.

フォームのデータは、指定されたすべてのグループに対して検証されます。

If the validation groups depend on the form's data a callable may be passed to the option. Symfony will then pass the form when calling it:

検証グループがフォームのデータに依存する場合、callable をオプションに渡すことができます。 symfony はそれを呼び出すときにフォームを渡します:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

// ...
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'validation_groups' => function (FormInterface $form) {
            $entity = $form->getData();

            return $entity->isUser() ? ['User'] : ['Company'];
        },
    ]);
}

See also

こちらもご覧ください

You can read more about this in How to Choose Validation Groups Based on the Submitted Data.

詳細については、提出されたデータに基づいて検証グループを選択する方法を参照してください。

Note

ノート

When your form contains multiple submit buttons, you can change the validation group depending on which button is used to submit the form.

フォームに複数の送信ボタンが含まれている場合、フォームの送信に使用されるボタンに応じて検証グループを変更できます。

If you need advanced logic to determine the validation groups have a look at How to Dynamically Configure Form Validation Groups.

検証グループを決定する高度なロジックが必要な場合は、フォーム検証グループを動的に構成する方法を参照してください。

In some cases, you want to validate your groups step by step. To do this, you can pass a GroupSequence to this option. This enables you to validate against multiple groups, like when you pass multiple groups in an array, but with the difference that a group is only validated if the previous groups pass without errors. Here's an example:

場合によっては、グループを段階的に検証する必要があります。これを行うには、GroupSequence をこのオプションに渡します。これにより、配列で複数のグループを渡すときのように、複数のグループに対して検証できますが、以前のグループがエラーなしで渡された場合にのみグループが検証されるという違いがあります。例を次に示します。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Validator\Constraints\GroupSequence;
// ...

class MyType extends AbstractType
{
    // ...
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'validation_groups' => new GroupSequence(['First', 'Second']),
        ]);
    }
}

See also

こちらもご覧ください

Read the article How to Sequentially Apply Validation Groups to find out more about this.

詳細については、検証グループを順次適用する方法の記事を参照してください。

Inherited Options

The following options are defined in the BaseType class. The BaseType class is the parent class for both the form type and the ButtonType, but it is not part of the form type tree (i.e. it cannot be used as a form type on its own).

次のオプションは、BaseType クラスで定義されています。BaseType クラスは、フォーム タイプと ButtonType の両方の親クラスですが、フォーム タイプ ツリーの一部ではありません (つまり、単独でフォーム タイプとして使用することはできません)。

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 オプションを使用します。

auto_initialize

type: boolean default: true

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

An internal option: sets whether the form should be initialized automatically. For all fields, this option should only be true for root forms. You won't need to change this option and probably won't need to worry about it.

内部オプション: フォームを自動的に初期化するかどうかを設定します。すべてのフィールドについて、このオプションはルート フォームに対してのみ true にする必要があります。このオプションを変更する必要はなく、おそらく気にする必要もありません。

block_name

type: string default: the form's name (see Knowing which block to customize)

タイプ: 文字列 デフォルト: フォームの名前 (カスタマイズするブロックを知るを参照)

Allows you to add a custom block name to the ones used by default to render the form type. Useful for example if you have multiple instances of the same form and you need to personalize the rendering of the forms individually.

フォーム タイプをレンダリングするためにデフォルトで使用されるブロック名にカスタム ブロック名を追加できます。たとえば、同じフォームの複数のインスタンスがあり、フォームのレンダリングを個別にパーソナライズする必要がある場合に役立ちます。

If you set for example this option to my_custom_name and the field is of type text, Symfony will use the following names (and in this order) to find the block used to render the widget of the field: _my_custom_name_widget, text_widget and form_widget.

たとえば、このオプションを my_custom_name に設定し、フィールドがテキスト型の場合、Symfony は次の名前を (この順序で) 使用して、フィールドのウィジェットをレンダリングするために使用されるブロックを見つけます: _my_custom_name_widget、text_widget、および form_widget。

block_prefix

type: string or null default: null (see Knowing which block to customize)

タイプ: 文字列または null デフォルト: null (カスタマイズするブロックを知るを参照)

Allows you to add a custom block prefix and override the block name used to render the form type. Useful for example if you have multiple instances of the same form and you need to personalize the rendering of all of them without the need to create a new form type.

カスタム ブロック プレフィックスを追加し、フォーム タイプのレンダリングに使用されるブロック名をオーバーライドできます。たとえば、同じフォームの複数のインスタンスがあり、新しいフォーム タイプを作成することなく、それらすべてのレンダリングをパーソナライズする必要がある場合に役立ちます。

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 に設定します。送信された値は無視されます。

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_html

type: boolean default: false

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

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

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

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 オプションを使用します。

translation_domain

type: string, null or false default: null

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

This is the translation domain that will be used for any label or option that is rendered for this field. Use null to reuse the translation domain of the parent form (or the default domain of the translator for the root form). Use false to disable translations.

これは、このフィールドに表示されるラベルまたはオプションに使用される翻訳ドメインです。 null を使用して、親フォームの翻訳ドメイン (またはルートフォームのトランスレータのデフォルト ドメイン) を再利用します。翻訳を無効にするには false を使用します。

label_translation_parameters

type: array default: []

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

The content of the label option is translated before displaying it, so it can contain translation placeholders. This option defines the values used to replace those placeholders.

label オプションのコンテンツは表示前に翻訳されるため、翻訳プレースホルダーを含めることができます。このオプションは、これらのプレースホルダーを置き換えるために使用される値を定義します。

Given this translation message:

この翻訳メッセージを考えると:
1
2
# translations/messages.en.yaml
form.order.id: 'Identifier of the order to %company%'

You can specify the placeholder values as follows:

プレースホルダー値は次のように指定できます。
1
2
3
4
5
6
$builder->add('id', null, [
    'label' => 'form.order.id',
    'label_translation_parameters' => [
        '%company%' => 'ACME Inc.',
    ],
]);

The label_translation_parameters option of children fields is merged with the same option of their parents, so children can reuse and/or override any of the parent placeholders.

子フィールドの label_translation_parameters オプションは、親の同じオプションとマージされるため、子は親のプレースホルダーを再利用および/またはオーバーライドできます。

attr_translation_parameters

type: array default: []

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

The content of the title and placeholder values defined in the attr option is translated before displaying it, so it can contain translation placeholders. This option defines the values used to replace those placeholders.

属性で定義されたタイトルとプレースホルダーの値の内容は、表示する前に翻訳されるため、翻訳プレースホルダーを含めることができます。このオプションは、これらのプレースホルダーを置き換えるために使用される値を定義します。

Given this translation message:

この翻訳メッセージを考えると:
1
2
3
# translations/messages.en.yaml
form.order.id.placeholder: 'Enter unique identifier of the order to %company%'
form.order.id.title: 'This will be the reference in communications with %company%'

You can specify the placeholder values as follows:

プレースホルダー値は次のように指定できます。
1
2
3
4
5
6
7
8
9
$builder->add('id', null, [
    'attr' => [
        'placeholder' => 'form.order.id.placeholder',
        'title' => 'form.order.id.title',
    ],
    'attr_translation_parameters' => [
        '%company%' => 'ACME Inc.',
    ],
]);

The attr_translation_parameters option of children fields is merged with the same option of their parents, so children can reuse and/or override any of the parent placeholders.

子フィールドの attr_translation_parameters オプションは、親の同じオプションとマージされるため、子は親のプレースホルダーを再利用および/またはオーバーライドできます。

priority

type: integer default: 0

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

Fields are rendered in the same order as they are included in the form. This option changes the field rendering priority, allowing you to display fields earlier or later than their original order.

フィールドは、フォームに含まれているのと同じ順序でレンダリングされます。このオプションは、フィールドのレンダリングの優先度を変更し、フィールドを元の順序よりも早くまたは遅く表示できるようにします。

This option will affect the view order only. The higher this priority, the earlier the field will be rendered. Priority can also be negative and fields with the same priority will keep their original order.

このオプションは、表示順序のみに影響します。この優先度が高いほど、フィールドが早くレンダリングされます。優先度を負にすることもでき、優先度が同じフィールドは元の順序を維持します。