symfony2 Sending options to a form class

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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' => [],
    ));
}


Got any symfony2 Question?