HiddenType Field

HiddenType Field

The hidden type represents a hidden input field.

Rendered as input hidden field
Overridden options
Inherited options
Parent type FormType
Class HiddenType

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

Overridden Options

compound

type: boolean default: false

This option specifies whether the type contains child types or not. This option is managed internally for built-in types, so there is no need to configure it explicitly.

error_bubbling

default: true

Pass errors to the root form, otherwise they will not be visible.

required

default: false

Hidden fields cannot have a required attribute.

Inherited Options

These options inherit from the 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:

$builder->add('body', TextareaType::class, [
    'attr' => ['class' => 'tinymce'],
]);
Use the row_attr option if you want to add these attributes to the the form type row element.

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.

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.

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

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;
  • If the violation is generated on an entry of an array or ArrayAccess object, the property path is [indexName];
  • You can construct nested property paths by concatenating them, separating properties by dots. For example: addresses[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',
    ],
]);

mapped

type: boolean default: true

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

property_path

type: PropertyPathInterface|string|null default: 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.

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:

$builder->add('body', TextareaType::class, [
    'row_attr' => ['class' => 'text-editor', 'id' => '...'],
]);
Use the attr option if you want to add these attributes to the the form type widget element.

New in version 4.3: The row_attr option was introduced in Symfony 4.3.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.