0

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?

3
  • 1
    I don't see any usage of createNamedBuilder in your example (which is the solution from the answer you linked to) Commented Jan 1, 2017 at 12:53
  • @Dekel I was referring to the second answer in that question, which is the way to go with Symfony3. Commented Jan 1, 2017 at 13:41
  • 1
    While I can sort of understand the desire to make urls prettier, plenty of sites have far uglier ones. In any event, a brute force solution is to POST the form then generate a pretty url in your controller and redirect. Trying to make the form class behave itself will make you go blind. Commented Jan 1, 2017 at 14:30

2 Answers 2

2

Try with:

// This function was to be ovveridden
public function getBlockPrefix()
{
    return '';
}
Sign up to request clarification or add additional context in comments.

2 Comments

I also think this is the right solution symfony does something like prefix + form-field to set the full name and if you use null you add a string to a null value which therefore still remains null
I must add that by overriding getBlockPrefix() whether this way or the one I have mentioned in my question, the id of form elements which are automatically created by Symfony would change. So it could potentially break some of your style and javascript code.
0

Changing form name is available via 'named' FormFactory methods:

  • \Symfony\Component\Form\FormFactoryInterface::createNamed
  • \Symfony\Component\Form\FormFactoryInterface::createNamedBuilder

Note that only the root (top-most) form can be with empty-name ''.

This is the official documentation regarding Changing the Form Name

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.