I wanted to modify the url of a submitted symfony form using the parameters. Tried many solutions from this platfrom and none of solution helped!
Current url is as follows:http://localhost:8000/search?app_bundle_search_form%5Bsearch%5D=qui&app_bundle_search_form%5Bbrand%5D=&app_bundle_search_form%5Bprice%5D=500%2C100000&app_bundle_search_form%5B_token%5D=BtA5bZb9HErUXzXFzGFbpEhlD6nD33zr7tKiPLxjpy4
I want it like `http://localhost:8000/search?search=qui?brand=?minprice=500?maxprice=100000
Here is my Controller:
 public function searchAction($searchTerm=null,Request $request)
{
    if ($request->getMethod() == 'GET') {
        $searchTerm = $request->query->get('app_bundle_search_form')['search'];
        $searchBrand = $request->query->get('app_bundle_search_form')['brand'];
        $price = $request->query->get('app_bundle_search_form')['price'];
        $price = explode(",", $price);
        $minPrice = $price[0];
        $maxPrice = $price[1];
        $em = $this->getDoctrine()->getManager();
        $search = $em->getRepository('AppBundle:Classified')->searchClassifieds($searchTerm, $searchBrand, $minPrice, $maxPrice);
    }
    return $this->render('search-result.html.twig', [
        'searchTerm' => $searchTerm,
        'results' => $search
    ]);
}
Form:
 public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->setMethod('GET')
        ->add('search', TextType::class,array(
            'required'=> false
        ))
        ->add('brand',EntityType::class,[
            'class'=>Brand::class,
            'placeholder'=>'Choose a brand',
            'required'=>false,
            'query_builder'=>function(BrandRepository $repo){
                return $repo->DistinctBrandValue();
            }
        ])
        ->add('price', TextType::class, array(
            'required' => true,
            'label' => 'Price',
            'attr' => [
                'data-slider-min' => '500',
                'data-slider-max' => '100000',
                'data-slider-step' => '2',
                'data-slider-value' => "[500,100000]",
            ]))
      ;
}
Routing
search:
path: /search
defaults:
    _controller: AppBundle:Search:search
requirements:
        methods: GET
Twig:
 {{ form_start(search, { action: path('search')}) }}
    {{ form_widget(search) }}
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">{% trans %}Search{% endtrans %}</button>
<a href="{{ path('search') }}" class="btn btn-primary">{% trans %}Cancel{% endtrans %}</a>
{{ form_end(search) }}
Thanks in advance!
EDIT: So what I done is I had to make the form name to null.
public function getBlockPrefix()
{
    return '';
}
And set the csrf protection to false.
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'csrf_protection' => false,
    ]);
}
And also changed the form name to null in the controller. Now the url looks a little better!
You could override the getName method in the formType and return an empty string, as example:
Form:
public function getName()
{
    return '';
}
Hope this help
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With