Search code examples
phpsymfonysymfony-forms

How to remove form name from URL when form is submitted?


I have a simple Symfony3 Form but as expected it produces an ugly url containing the form name when the form is GETed. Something like this:

...php?party_form%5Bplace%5D=Milan&party_form%5Bdate%5D=01%2F01%2F2017&party_form%5Bsave%5D=&party_form%5B_token%5D=KMN745JpTUyZZQSRnP5kd6YHQnQhAlU9eHtMwZ-zi7g

I would like to have the party_form removed from the url.

Following this question I made the following changes:

class PartyForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->setMethod('GET')
            ->add( $builder
                ->create('place', TextType::class)
                ->addModelTransformer(new CallbackTransformer(
                    function ($placesAsArray) {
                        // transform the array to a string
                        if ($placesAsArray) {
                            return implode(', ', array_values($placesAsArray));
                        }
                    },
                    function ($placesAsString) {

                        // transform the string back to an array
                        $rawPlaceArray = explode(', ', $placesAsString);

                        // If the place doesn't have city, province and country don't search for it.
                        // validation constraints on place will throw an error.
                        if (count($rawPlaceArray) == 3) {
                            $keys = array('city', 'province', 'country');
                            return array_combine($keys, $rawPlaceArray);
                        }
                    }
                ))

            )
            ->add('date', DateType::class,
                  array(
                      'widget' => 'single_text',
                      'format' => 'dd/MM/yyyy',
                  )
            )
            ->add('save', SubmitType::class,
                  array(
                      'label' => 'Find List',
                  )
            );

    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Frontend\Event',
            'required' => false
        ));
    }

   // This function was to be ovveridden
    public function getBlockPrefix()
    {
        return null;
    }

But now the form is submitted with fields null! What am I missing here?


Solution

  • Try with:

    // This function was to be ovveridden
    public function getBlockPrefix()
    {
        return '';
    }