CitizenG1
CitizenG1

Reputation: 317

Unable to guess how to get a Doctrine instance from the request information for parameter "class"

I'm trying to send an email with a token in order to resend an automated password. But in my view i'm retrieving the following error:

/**
     * @Route("/forget-email/", name="forget", methods="GET|POST")
     */
     public function emailrestore(Request $request, User $user, \Swift_Mailer $mailer)
     {
        $url = "test";
        $form = $this->createForm(ForgetPasswordType::class, $user);
        $form->handleRequest($request);
        $email = $form['email']->getData();
        $user = $this->getDoctrine()
        ->getRepository(User::class)
        ->find($email);
        if ( $email === $user ) {
            $mail = (new \Swift_Message('Hello Email'))
                ->setFrom('[email protected]')
                ->setTo($email)
                ->setBody(
                    $this->renderView(
                        // templates/emails/registration.html.twig
                        'emails/registration.html.twig',array('url' => $url,)
                    ),
                    'text/html'
                );
                $mailer->send($mail);
        } else{
            var_dump("$email");
        }

        return $this->render('forget/email.html.twig', [
            'form' => $form->createView(),
            'error' => null,
        ]);
     }

Inside my entity i have email as unique

* @UniqueEntity(fields="email", message="Email already taken")

I'm retrieving the following error: "Unable to guess how to get a Doctrine instance from the request information for parameter "user"." Why?

Thanks for your explanation for advance

Upvotes: 0

Views: 3728

Answers (2)

Asez
Asez

Reputation: 1

You need to specify an initial value for your $user variable Try this :

public function emailrestore(Request $request, User $user, \Swift_Mailer $mailer)

Upvotes: 0

Yoann MIR
Yoann MIR

Reputation: 821

You have "User $user" in your action parameter but you don't have a parameter 'user' in your request, my guess is your ParamConverter can't convert this one into a User object because of that.

make your route a GET route formatted like:

/**
 * @Route("/forget-email/{user}", name="forget", methods="GET")
 */

Where user is whatever you want (mail, username, uid, etc ...) has long as it allows you tyo retrieve your user object and have your paramconverter converting it into a Doctrine object ...

Upvotes: 1

Related Questions