简体   繁体   中英

Stop Symfony form from redirecting after submit

I have a form that is rendered in a pop-up. When the form is submitted and valid, the data is saved and I'm redirected to the route of the form. How could I stop that redirection? I don't want to render another page, my goal is closing the pop-up after a valid submit.

public function addFeedbackAction(Request $request)
 {
   $view = View::create();

   $feedback = new Feedback();
   $feedbackService = $this->get('main.feedback.service');
   $form = $this->createForm(new FeedbackType(), null, ['action' => 'feedback']);
   $form->handleRequest($request);

   if ($form->isValid()) {
       $formData = $form->getData();
       $feedbackService->create($formData, $feedback);

       return null;
   }
   $view
     ->setData($form)
     ->setTemplateData($form)
     ->setTemplate('MainBundle:Modals:feedback.html.twig');

   return $view;

You can catch submit event with Javascript.

You can find an exemple using JQuery here: https://api.jquery.com/submit/

Send data using AJAX

$.ajax({
        type: "POST",
        url: "yourControllerHere",
        data: form.serialize(),
        beforeSend: function() {
            //TODO: Loading
        },
        error: function( xhr){

        },
        success: function( data ){          

         }
    });

Then you can process what you gonna do in success: function (data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM