Editing
Unmapped Form Fields in Symfony
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
[[Category:Symfony]] [[Category:CMS]] [[Category:Web Development]] == 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 `next` field does not correspond to an entity class property. ** `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". ** `empty_data` marks the value that will be submitted if none of the choices in the form are selected. ** `expanded => true` causes the "choice" field type to be rendered as a list of radio buttons. <syntaxhighlight lang="php" highlight="13,24"> 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'); } </syntaxhighlight> == Accessing the unmapped field value in the controller == The field value can be accessed with `$form->get('next')->getData();` ''N.B. From the documentation it seems like the `empty_data` setting should represent a default value for the the list of options, but this doesn't work.'' == Setting a default value for unmapped fields == <syntaxhighlight lang="php"> $form->get('next')->setData($next); </syntaxhighlight>
Summary:
Please note that all contributions to Littledamien Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Littledamien Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information