symfony2 Sending options to a form class A real-world example from a Household controller


Example

Background: The Household entity includes a set of options, each of which is an entity that is managed in an admin backend. Each option has a boolean enabled flag. If a previously enabled option is set to disabled it will need to be persisted in later Household edits, but cannot be edited away. To accomplish this the field definition in the form class will display field as a disabled choice field if the option has enabled = false (but is persisted because the submit button triggers a javascript that removes the disabled attribute.) The field definition also prevents disabled options from being displayed.

The form class then needs to know, for a given Household entity, which of its options have been disabled. A service has been defined that returns an array of the names of option entities that have been disabled. That array is $disabledOptions.

    $formOptions = [
        'disabledOptions' => $disabledOptions,
        ];
    $form = $this->createForm(HouseholdType::class, $household, $formOptions);