0

I am trying to use some data given by query string in my form. I have searched the internet and found a good explaination (http://bicknoyle.com/2014/04/passing-query-string-params-form-symfony/):

class RegistrationFormType extends BaseType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        $builder->add('refer', 'user_refer_type');

        $builder->addEventListener(FormEvents::POST_SET_DATA, function(FormEvent $event) use ($options) {
            if (isset($options['request'])) {
                $form = $event->getForm();
                $form->get('source')->setData($options['request']->query->get('source'));
            }
        });
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setOptional(array(
            'request'
        ));

        $resolver->setAllowedTypes(array(
            'request' => 'Symfony\Component\HttpFoundation\Request'
        ));
    }

I found another way using the request scope:

class RegistrationFormType extends BaseType
{

    private $request = null;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        $builder->add('refer', 'user_refer_type');

        $builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event)
        {
            $form = $event->getForm();
            $form->get('refer')->setData($this->request->query->get('refer'));
        });
    }

I have some questions about the code:

  1. What is the "better" way to implement such thing?
    In general I can summarize, the first approach needs access to the controller to pass the request object and the second needs request scope.
  2. Did I miss another good way?
1
  • I think that the second way it's "better", because you don't have to use setOptional and setAllowedTypes so less code Commented Aug 14, 2014 at 23:12

1 Answer 1

2

I suggest to use the second approach, but instead of injecting the Request object you should inject RequestStack service according to this post..

Here is some eaxmple how I usually do it:

class CustomType extends AbstractType
{
    /**
     * @var RequestStack
     */
    private $requestStack;

    /**
     * @param RequestStack $requestStack
     */
    public function __construct(RequestStack $requestStack)
    {
        $this->requestStack = $requestStack;
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $currentRequest = $this->requestStack->getCurrentRequest();

        ...

        $builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) use ($currentRequest)
        {
            $form = $event->getForm();
            $form->get('refer')->setData($currentRequest->query->get('refer'));
        });
    }

    ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.