repeated Field Type

This is a special field "group", that creates two identical fields whose
values must match (or a validation error is thrown). The most common use
is when you need the user to repeat their password or email to verify
accuracy.

Upon a successful form submit, the value entered into both of the "password"
fields becomes the data of the password key. In other words, even though
two fields are actually rendered, the end data from the form is just the
single value (usually a string) that you need.

The most important option is type, which can be any field type and determines
the actual type of the two underlying fields. The options option is
passed to each of those individual fields, meaning - in this example - any
option supported by the password type can be passed in this array.

The names first and second are the default names for the two
sub-fields. However, these names can be controlled via the first_name
and second_name options. If you've set these options, then use those
values instead of first and second when rendering.

One of the key features of the repeated field is internal validation
(you don't need to do anything to set this up) that forces the two fields
to have a matching value. If the two fields don't match, an error will be
shown to the user.

The invalid_message is used to customize the error that will
be displayed when the two fields do not match each other.

This is the actual field name to be used for the first field. This is mostly
meaningless, however, as the actual data entered into both of the fields
will be available under the key assigned to the repeated field itself
(e.g. password). However, if you don't specify a label, this field
name is used to "guess" the label for you.

This options array will be passed to each of the two underlying fields.
In other words, these are the options that customize the individual field
types. For example, if the type option is set to password, this
array might contain the options always_empty or required - both
options that are supported by the password field type.

type: mixeddefault: Defaults to field of the underlying object (if there is one)

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

1
2
3

$builder->add('token','hidden',array('data'=>'abcdef',));

Note

The default values for form fields are taken directly from the underlying
data structure (e.g. an entity or an array). The data option overrides
this default value.

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: