简体   繁体   中英

User submit form, but don't save until he register or login

I have many forms that user fill on single page, but after user make post, controller will check if form is valid and if user is already logged in then save info.

Need help with

If user is not logged in, then redirect him to registration page where he sign up and after sign up he will be auto login, then it should come back to same controller with posted values so it can be saved.

  • $form->isValid && !$this->getUser() then redirect user to sign up page with this form values saved in session?
  • after user sign up or login, then come back to previous controller (without any further action) it should auto saved the previously submitted form. since it was $form->isValid()

I'm confused, should I save form values in session and redirect? would it save model values properly?

Also how should I redirect user to registration page, while telling Symfony to come back to same page with post method after login.

对我来说,最简单的解决方案是将两个页面都作为div放在同一页面上,并使用javascript隐藏未提交的表单,因此您无需存储和重新填充值。

I think you have to follow this structure :

Case : After submit form page redirect to register/login page.

1.) Fill the form and after submit form convert post data into serialize array. here .

2.) paste that serialize array in hidden fields or session (Register/Login page).

3.) fill the register form, after registration, send back to previous form page and send hidden data to that page and unserialize them. here

Suggestion : IF you want your post data not show in page then you can use urlencode and urldecode function or any other encoding method you can use.

Note : To use session or cookie to send post data from one page to anther page is bad practise. It will be on all pages until you unset/delete it.

redirect to login page from controller manually , use following:

    $session->set('_security.user_firewall.target_path', $request->getUri());

    throw new AuthenticationException(); // use Symfony\Component\Security\Core\Exception\AuthenticationException;

redirect from registration page , use following:

         $key = '_security.user_firewall.target_path'; #where "user_firewall" is your firewall name

        //check if the referrer session key has been set
        if ($this->container->get('session')->has($key))
        {
            //set the url based on the link they were trying to access before being authenticated
            $url = $this->container->get('session')->get($key);

            //remove the session key
            $this->container->get('session')->remove($key);
        }
        // if the referrer key was never set, redirect to a default route
        else{
            $url = $this->generateUrl($routeName);
        }

        return new RedirectResponse($url);

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