symfony2Sending options to a form class


Syntax

  • $form = $this->createForm(HouseholdType::class, $household, $formOptions);

Parameters

ParameterDefinition
HouseholdType::classcustom form class for the Household entity
$householdan instance of the Household entity (usually created by $household = new Household();)
$formOptionsan array of user-defined options to be passed to the form class, e.g.,$formOptions = array('foo' => 'bar');

Remarks

When you create a form class the form fields are added in the public function buildForm(FormBuilderInterface $builder, array $options) {...} function. The $options parameter includes a set of default options such as attr and label. To enable your custom options to be available in the form class the options need to be initialized in configureOptions(OptionsResolver $resolver)

So for our real-world example:

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\Household',
        'disabledOptions' => [],
    ));
}