Supplying arguments to child form thats a service

2019-08-29 11:56发布

I'm trying to make a child form more dynamic (besides already being highly reusable by making it a service) by providing parameters to it.

However I'm having trouble providing parameters to a choice class that the child form uses.

TL;DR: The form StateListType inherits the choice parent. Is it possible to change/override the default 'choice_list' from the buildForm method inside StateListType?

The comments in the following code explain what I'd like to do.

<?php

namespace Acme\DemoBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class PersonFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('State', 'statelist'),//statelist is a form service.
            ->add('HomeState', 'statelist')//use form service here too
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\DemoBundle\Entity\Person'
        ));
    }

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

The following code is a form that the service container takes care of injecting the proper dependency to it.

<?php

namespace Acme\DemoBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class StateListType extends AbstractType
{
    protected $ChoiceList;

    public function __construct(ChoiceListInterface $Choices)
    {
        //If I'm right I can't use another parameter
        //to call from the parent form since it's already a service?
        $this->ChoiceList = $Choices;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        //Don't usually need this method at all.
        //However if I need to provide arguments
        //I can set 'custom_argument'
        //in the setDefaultOptions method below.

        //Then I could use $options['custom_argument']
        //from the parent form to provide arguments to
        //$this->ChoiceList->doSomething($options['custom_argument')
        //and then replacing the default 'choice_list' with this?
        //Just not sure how or if even possible to do it from here.
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver
            ->setDefaults(
                array(
                    //Could use $arg this by getting options from the constructor.
                    //But since it's a service I can't supply dynamic arguments.
                    'choice_list' => $this->ChoiceList->doSomething($arg),//Returns itself after doing something.
                    'empty_value' => 'select a state',
                    'custom_argument' => null
                )
            );

    }

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

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

Extra info as per request in the comments:

Service:

StateListChoice:
    class: Acme\DemoBundle\Form\Extension\StateListChoices

StateListType:
    class: Acme\DemoBundle\Form\StateListType
    arguments: ["@StateListChoices"]
    tags:
            {name: form.type, alias: statelist}

Custom choice class:

StateListChoices.php

class StateListChoices extends LazyChoiceList implements ChoiceListInterface
{
    ...
    public function doSomething($argument)
    {
         //Does something with argument
         return $this; //Returns ChoiceListInterface object.
    }
    ...
}

标签: forms symfony
1条回答
Rolldiameter
2楼-- · 2019-08-29 12:40

If your buildForm() in StateListType I don't know why not just build the form in PersonType directly?

You can also define PersonType as a service:

PersonType:
    class: Acme\DemoBundle\Form\PersonType
    arguments: ["@StateListChoices"]
    tags:
            {name: form.type, alias: person}

Then use the choices services there directly:

use Acme\DemoBundle\Form\Extension\StateListChoices


class PersonType extends AbstractType
{
    public function __construct(ChoiceListInterface $choicesList)
    {
        $this->choicesList = $choicesList;
    }

    private function getChoices($argument = null)
    {
        return $this->choicesList->doSomething($argument);
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
          $builder
            ->add('State', 'choice', array(
                'choice_list' => $this->getChoices($options['custom_argument']),
            ))
            ->add('HomeState', 'statelist', array(
                'choice_list' =>  $this->getChoices();
            ));
    }
查看更多
登录 后发表回答