Validation password confirmation in Symfony

253 Views Asked by At

I have a problem when I try to create a validation of my registration form, I get an error

Invalid property path "passwordConfirmation" provided to "Symfony\Component\Validator\Constraints\EqualTo" constraint: Can't get a way to read the property "passwordConfirmation" in class "Symfony\Component\Form\Form".

The password field is in the User entity

<?php

namespace App\Form\Registration;

use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\EqualTo;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;

class RegistrationForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('email', EmailType::class, [
                'constraints' => [
                    new NotBlank([
                        'message' => 'Please enter an email address',
                    ]),
                    new Email([
                        'message' => 'Please enter a valid email address',
                    ]),
                ],
            ])
            ->add('password', PasswordType::class, [
                'constraints' => [
                    new NotBlank([
                        'message' => 'Please enter a password',
                    ]),
                    new Length([
                        'min' => 6,
                        'minMessage' => 'Your password should be at least {{ limit }} characters',
                        'max' => 255,
                    ]),
                ],
            ])
            ->add('passwordConfirmation', PasswordType::class, [
                'mapped' => false,
                'constraints' => [
                    new NotBlank([
                        'message' => 'Please confirm your password',
                    ]),
                    new Length([
                        'min' => 6,
                        'minMessage' => 'Your password confirmation should be at least {{ limit }} characters',
                        'max' => 255,
                    ]),
                    new EqualTo([
                        'propertyPath' => 'password',
                        'message' => 'Your password confirmation does not match the entered password',
                    ]),
                ],
            ])
            ->add('save', SubmitType::class);
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => User::class,
            'translation_domain' => 'forms'
        ]);
    }
}
namespace App\Entity;

use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Types\UuidType;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Validator\Constraints\Email;

#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: '`user`')]
class User implements PasswordAuthenticatedUserInterface
{
    #[ORM\Id]
    #[ORM\Column(name: 'id', type: UuidType::NAME)]
    private ?Uuid $id = null;

    #[Email]
    #[ORM\Column(length: 180, unique: true)]
    private ?string $email = null;

    #[ORM\Column]
    private ?string $password = null;

    #[ORM\ManyToMany(targetEntity: Roles::class, mappedBy: 'user_id')]
    private Collection $userRoles;

    #[ORM\Column]
    private ?bool $is_deleted = null;

    #[ORM\Column]
    private ?\DateTimeImmutable $created_at = null;

    #[ORM\Column(nullable: true)]
    private ?\DateTimeImmutable $updated_at = null;

    #[ORM\Column(nullable: true)]
    private ?\DateTimeImmutable $email_verify_at = null;

    public function __construct()
    {
        $this->userRoles = new ArrayCollection();
    }

 etc code in entity user

This error does not occur if I create a field in the User password Confirmation entity and write it in the EqualTo attributes, but with this approach, a column is created in the database and validation does not work through the form class anyway

I would be very grateful if you could tell me how to solve this problem and in general which option is more correct to use for form validations?

2

There are 2 best solutions below

0
Skuti On

You should use the Symfony Repeated field type instead https://symfony.com/doc/4.4/reference/forms/types/repeated.html

0
Dan On

You can use Repeated type like this:

->add('password', RepeatedType::class, [
    'type' => PasswordType::class,
]);