WARNING: You are browsing the documentation for Symfony 2.0 which is not maintained anymore. Consider upgrading your projects to Symfony 4.2.

The Abstract "field" Type

2.0 version
Unmaintained

The Abstract "field" Type

The field form type is not an actual field type you use, but rather functions as the parent field type for many other fields.

The field type predefines a couple of options:

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.

disabled

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
use Symfony\Component\Form\TextField

$field = new TextField('status', array(
    'data' => 'Old data',
    'disabled' => true,
));
$field->submit('New data');

// prints "Old data"
echo $field->getData();

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.

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

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'),
));

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