I am trying to encode a password in Symfony and after carefully following the documentation here it still seems like I am doing something wrong.
This is my RegisterController.php:
<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\Type\UserType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
class RegisterController extends AbstractController
{
private $passwordEncoder;
public function __construct(UserPasswordEncoderInterface $passwordEncoder)
{
$this->passwordEncoder = $passwordEncoder;
}
/**
* @Route("/register", name="user.register")
*/
public function create(Request $request)
{
$user = new User();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user->setPassword(
$this->passwordEncoder->encodePassword( $user, $user->getPassword() )
);
The above returns the following error:
Neither the property "plainPassword" nor one of the methods "getPlainPassword()", "plainPassword()", "isPlainPassword()", "hasPlainPassword()", "__get()" exist and have public access in class "App\Entity\User".
And here is my Register.twig.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{% block title %}Register{% endblock %}</title>
{# {% block stylesheets %}{% endblock %} #}
</head>
<body>
<div class="container">
<h2 class="form-signin-heading">Welcome, please register below</h2>
{{ form(form) }}
</div>
</body>
</html>
And finally I have this setup in my security.yaml
file:
security:
encoders:
App\Entity\User:
algorithm: auto
I guess it's something simple that I'm overlooking but I couldn't get it to work. It is my first time with Symfony
.