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?

Community
  • 1
  • 1
tutak
  • 1,120
  • 1
  • 15
  • 28
  • I don't see any usage of `createNamedBuilder` in your example (which is the solution from the answer you linked to) – Dekel Jan 01 '17 at 12:53
  • @Dekel I was referring to the second answer in that question, which is the way to go with Symfony3. – tutak Jan 01 '17 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. – Cerad Jan 01 '17 at 14:30

1 Answers1

2

Try with:

// This function was to be ovveridden
public function getBlockPrefix()
{
    return '';
}
COil
  • 7,201
  • 2
  • 50
  • 98
  • 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 – Nickolaus Jan 04 '17 at 12:27
  • 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. – tutak Jan 06 '17 at 08:21