Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Reference
  4. Types
  5. form Field Type
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia

Table of Contents

  • data
  • required
  • constraints
  • cascade_validation
  • read_only
  • disabled
  • trim
  • mapped
  • property_path
  • attr
  • translation_domain
  • block_name
    • virtual

form Field Type

Edit this page

Warning: You are browsing the documentation for Symfony 2.2, which is no longer maintained.

Read the updated version of this page for Symfony 6.2 (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 name

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 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:

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.

block_name

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

Allows you to 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 the forms individually.

virtual

See How to use the Virtual Form Field Option

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Be safe against critical risks to your projects and businesses

Be safe against critical risks to your projects and businesses

Symfony Code Performance Profiling

Symfony Code Performance Profiling

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of Lopton, a Symfony contributor

Thanks Lopton for being a Symfony contributor

1 commit • 34 lines changed

View all contributors that help us make Symfony

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Learn how to contribute

Symfony™ is a trademark of Symfony SAS. All rights reserved.

  • What is Symfony?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • Symfony Releases
    • Security Policy
    • Logo & Screenshots
    • Trademark & Licenses
    • symfony1 Legacy
  • Learn Symfony
    • Symfony Docs
    • Symfony Book
    • Reference
    • Bundles
    • Best Practices
    • Training
    • eLearning Platform
    • Certification
  • Screencasts
    • Learn Symfony
    • Learn PHP
    • Learn JavaScript
    • Learn Drupal
    • Learn RESTful APIs
  • Community
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia