form Field Type
Edit this pageWarning: You are browsing the documentation for Symfony 2.1, which is no longer maintained.
Read the updated version of this page for Symfony 6.3 (the current stable version).
form Field Type
See FormType.
The form
type predefines a couple of options that are then available
on all fields.
data
type: mixed default: 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',
));
required
type: Boolean
default: true
If true, an HTML5 required attribute will be rendered. The corresponding
label
will also render with a required
class.
This is superficial and independent from validation. At best, if you let Symfony guess your field type, then the value of this option will be guessed from your validation information.
constraints
type: array or Constraint default: 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.
cascade_validation
type: Boolean default: false
Set this option to true
to force validation on embedded form types.
For example, if you have a ProductType
with an embedded CategoryType
,
setting cascade_validation
to true
on ProductType
will cause
the data from CategoryType
to also be validated.
Instead of using this option, you can also use the Valid
constraint in
your model to force validation on a child object stored on a property.
read_only
2.1
The read_only
option was changed in 2.1 to render as a readonly
HTML attribute. Previously, it rendered as a disabled
attribute.
Use the disabled option if you need the old behavior.
type: Boolean
default: false
If this option is true, the field will be rendered with the readonly
attribute so that the field is not editable.
disabled
2.1
The disabled
option is new in version 2.1
type: boolean
default: 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.
trim
type: Boolean
default: 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.
mapped
type: boolean
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: any
default: the field's value
Fields display a property value of the form's domain object by default. When the form is submitted, the submitted value is written back into the object.
If you want to override the property that a field reads from and writes to,
you can set the property_path
option. Its default value is the field's
name.
If you wish the field to be ignored when reading or writing to the object
you can set the property_path
option to false
, but using
property_path
for this purpose is deprecated, you should use the
mapped
option.
2.1
Since 2.1, the mapped
option has been added for this use-case.
attr
type: array default: Empty array
If you want to add extra attributes to HTML field representation
you can use attr
option. It's an associative array with HTML attribute
as a key. This can be useful when you need to set a custom class for some widget:
1 2 3
$builder->add('body', 'textarea', array(
'attr' => array('class' => 'tinymce'),
));
translation_domain
type: string
default: messages
This is the translation domain that will be used for any labels or options that are rendered for this field.