2
votes

This is the error

Argument 1 passed to Symfony\Component\Security\Core\Encoder\UserPasswordEncoder::encodePassword() must be an instance of Symfony\Component\Security\Core\User\UserInterface, instance of App\Entity\User given, called in C:\Users\willi\Desktop\Crowdin\src\Controller\CompteController.php on line 44

CompteController.php :

  /** 
 * @Route("/inscriptions", name="inscription")
 */
public function inscriptions(Request $request, ObjectManager $manager, UserPasswordEncoderInterface $encoder) {
    $user = new User();

    $form = $this->createForm(RegistrationType::class, $user);

    $form->handleRequest($request);

    if($form->isSubmitted() && $form->isValid()) {

        $hash = $encoder->encodePassword($user, $user->getPassword());

        $user->setPassword($hash);

        $manager->persist($user);
        $manager->flush();

        return $this->redirectToRoute('login');
    }

    return $this->render('compte/inscriptions.html.twig', [
        'form' => $form->createView()
    ]);
}

User.php :

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 * @UniqueEntity(
 *  fields={"email"},
 *  message="L'email que vous avez indiqué est déja utilisé !"
 * )
 */
class User implements UserInterface
{
1
App\Entity\User must either implement Symfony\Component\Security\Core\User\UserInterface or a descendant interface, or extend a class that does.Sammitch
i already do thisNoRage
Are you sure you implemented the right UserInterface? What does var_dump($user instanceof Symfony\Component\Security\Core\User\UserInterface) say?jrswgtr
How did you implement it?Ibu
There's more to it than that.Don't Panic

1 Answers

3
votes

It sounds like you're only importing the interface into the local namespace, but not actually implementing it.

namespace foo;         // namespace declaration
use Bar\UserInterface; // namespace import, does not actually implement it.

class User implements UserInterface { // <- this is the important part
  // now you need to implement the methods specified in the interface.
}

Alternatively, assuming class Bar\User implements Bar\UserInterface:

class User extends Bar\User {
  // now you only need to define/redefine the specific things that you want to
}