Unmapped Form Fields in Symfony: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
| Line 13: | Line 13: | ||
* The `next` field does not correspond to an entity class property. | * The `next` field does not correspond to an entity class property. | ||
** `mapped => false` marks this as an unmapped field. | ** `mapped => false` marks this as an unmapped field. | ||
** The field type is `next_nav_choice`. | |||
*** This is a custom field type that extends choice without any extra functionality. | |||
*** It is simply a hook to differentiate this set of radio buttons from other ones in the front-end template, since these are always going to represent navigation choices. | |||
** `placeholder => null` removes an option inserted by Symfony that explicitly sets the choice to "no choice". | ** `placeholder => null` removes an option inserted by Symfony that explicitly sets the choice to "no choice". | ||
** `empty_data` marks the value that will be submitted if none of the choices in the form are selected. | ** `empty_data` marks the value that will be submitted if none of the choices in the form are selected. | ||
Revision as of 06:54, 19 February 2015
Objective
Use a form class to build out a form to edit an entity.
Add fields to the form to control navigation after submitting the form, for example.
These extra fields don't have equivalents in the entity class. It cannot be used to inspect their values in the submitted form data.
Form Class
- The first two form fields added to the form builder correspond to properties of the entity class.
- The
nextfield does not correspond to an entity class property.mapped => falsemarks this as an unmapped field.- The field type is
next_nav_choice.- This is a custom field type that extends choice without any extra functionality.
- It is simply a hook to differentiate this set of radio buttons from other ones in the front-end template, since these are always going to represent navigation choices.
placeholder => nullremoves an option inserted by Symfony that explicitly sets the choice to "no choice".empty_datamarks the value that will be submitted if none of the choices in the form are selected.expanded => truecauses the "choice" field type to be rendered as a list of radio buttons.
class InvoiceType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('number', 'integer', array(
'label' => 'Invoice Number',
))
->add('amount', 'money', array(
'currency'=> 'USD',
))
// ...
->add('next', 'next_nav_choice', array(
'label' => 'After saving the invoice go to:',
'placeholder' => null,
'required' => false,
'choices' => array(
'list' => 'invoice listings',
'details' => 'view the invoice',
'add' => 'add another invoice',
),
'empty_data' => array('list'),
'expanded' => true,
'mapped' => false,
))
->add('save', 'submit');
}