Form/Extension/FormTypeRedirectExtension.php
public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // Abort if not enabled
        if (! (array_key_exists('_redirect_enabled', $options) && $options['_redirect_enabled'] === true)) {
            return;
        }

        // Retrieve the _redirect path from request (GET)
        $redirectPath = null;
        if (null !== $request = $this->requestStack->getCurrentRequest()) {
            $redirectPath = $request->query->get('_redirect', null);
        }

        // Add the hidden field
        $builder->addEventListener(
            FormEvents::PRE_SET_DATA,
            function (FormEvent $event) {
                $form = $event->getForm();
                // Only "root" form
                if(null !== $form->getParent()) {
                    return;
                }
                $form->add('_redirect', 'hidden', ['mapped' => false]);
            }
        );

        // Sets the data
        $builder->addEventListener(
            FormEvents::POST_SET_DATA,
            function (FormEvent $event) use ($redirectPath, $request) {
                $form = $event->getForm();
                // Only "root" form
                if(null !== $form->getParent()) {
                    return;
                }
                // If form has been posted => retrieve _redirect path from request (POST)
                if(null === $redirectPath && null !== $request) {
                    $datas = $request->request->get($form->getName(), ['_redirect' => null]);
                    if (array_key_exists('_redirect', $datas)) {
                        $redirectPath = $datas['_redirect'];
                    }
                }
                $form->get('_redirect')->setData($redirectPath);
            }
        );
    }
public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // Abort if not enabled
        if (! (array_key_exists('_redirect_enabled', $options) && $options['_redirect_enabled'] === true)) {
            return;
        }

        // Retrieve the _redirect path from request (GET)
        $redirectPath = null;
        if (null !== $request = $this->requestStack->getCurrentRequest()) {
            $redirectPath = $request->query->get('_redirect', null);
        }

        // Add the hidden field
        $builder->addEventListener(
            FormEvents::PRE_SET_DATA,
            function (FormEvent $event) {
                $form = $event->getForm();
                // Only "root" form
                if(null !== $form->getParent()) {
                    return;
                }
                $form->add('_redirect', 'hidden', ['mapped' => false]);
            }
        );

        // Sets the data
        $builder->addEventListener(
            FormEvents::POST_SET_DATA,
            function (FormEvent $event) use ($redirectPath, $request) {
                $form = $event->getForm();
                // Only "root" form
                if(null !== $form->getParent()) {
                    return;
                }
                // If form has been posted => retrieve _redirect path from request (POST)
                if(null === $redirectPath && null !== $request) {
                    $datas = $request->request->get($form->getName(), ['_redirect' => null]);
                    if (array_key_exists('_redirect', $datas)) {
                        $redirectPath = $datas['_redirect'];
                    }
                }
                $form->get('_redirect')->setData($redirectPath);
            }
        );
    }
{@inheritDoc}