Unmapped Form Fields in Symfony: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
(Created page with "== 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 fie...")
 
No edit summary
Line 6: Line 6:


These extra fields don't have equivalents in the entity class. It cannot be used to inspect their values in the submitted form data.
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 ==
<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>

Revision as of 06:46, 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

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