Symfony2 - Change selection in custom field Type from within formBuilder

I created my own "status" form type by expanding the selection field ( after the tutorial in the cookbook ). The selection of the select box is automatically populated from the parameters that are entered through the service. However, I would like to customize this status field to use a simple or advanced set of options, either a simple status list (inactive, live) or a more complete status list (inactive, live, deleted, deleted).

My code below changes successfully $options['choices']

based on my $ options ['customOptions'], but these changes do not affect the final options of the select box that are displayed.

How can you change the parameters of a field choice

in the formBuilder?

parameters.yml

parameters:
    gutensite_component.options.status:
        0: Inactive
        1: Live
    gutensite_component.options.status_preview:
        2: "Preview Only"
        3: "Live Only (delete on sync)"
    gutensite_component.options.status_delete:
        -1: "Delete (soft)"
        -2: Deleted

      

services.yml

services:
    gutensite_component.form.type.status:
        class: Gutensite\ComponentBundle\Form\Type\StatusType
        arguments:
            - "%gutensite_component.options.status%"
            - "%gutensite_component.options.status_preview%"
            - "%gutensite_component.options.status_delete%"
        tags:
            - { name: form.type, alias: "status" }

      

Form Type

class StatusType extends AbstractType
{

    /**
     * $statusChoices are passed in from services.yml that initiates this as a global service
     * @var array
     */
    private $statusChoices;
    private $statusChoicesPreview;
    private $statusChoicesDelete;

    public function __construct(array $statusChoices, array $statusChoicesPreview,
        array $statusChoicesDelete) {
        $this->statusChoices = $statusChoices;
        $this->statusChoicesPreview = $statusChoicesPreview;
        $this->statusChoicesDelete = $statusChoicesDelete;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'customOptions' => array(),
            // By default it will use the simple choices
            'choices' => $this->statusChoices
        ));
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // Set extra choices based on customOptions
        if(!empty($options['customOptions']['showPreview'])) {
            $options['choices'] = $options['choices'] + $this->staftusChoicesPreview;
        }
        if(!empty($options['customOptions']['showDelete'])) {
            $options['choices'] = $options['choices'] + $this->statusChoicesDelete;
        }

        // The $options['choices'] ARE successfully modified and passed to the parent, but
        // have no effect on the options used in the form display

        // Include Regular Choice buildForm
        parent::buildForm($builder, $options);
    }




    public function getParent() {
        return 'choice';
    }

    public function getName() {
        return 'status';
    }

}

      

controller

$builder->add('status', 'status', array(
    'label'     => 'Status',
    'required'  => TRUE,
    'customOptions' => array(
        'showPreview' => true
    )
));

      

+3


source to share


1 answer


You can pass an array of options to the form class from the controller:

In the controller:

$myArray = array('0' => 'foo', '1' => 'bar', ...);
...
$form = $this->createForm(new SomeFormType($myArray), ...);

      



In the form class:

class SomeFormType extends AbstractType
{

private $choiceArray;

public __construct($array)
{
    $this->choiceArray = $array;
}

    public function buildForm(FormBuilderInterface $builder, array $options) 
    {
        $builder
            ->add('status', 'choice', array(
                 'choices' => $this->choiceArray,
                  ...
             )
        ...
    }
...
}

      

0


source







All Articles