Symfony 中的验证密码确认

问题描述 投票:0回答:1
当我尝试创建注册表单验证时遇到问题,出现错误

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".
密码字段位于用户实体中

<?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
如果我在用户密码确认实体中创建一个字段并将其写入 EqualTo 属性中,则不会发生此错误,但使用这种方法,会在数据库中创建一个列,并且无论如何验证都无法通过表单类进行工作

如果您能告诉我如何解决这个问题以及一般情况下哪个选项更适合用于表单验证,我将不胜感激?

symfony symfony4 symfony-forms
1个回答
0
投票
您应该使用 Symfony Repeated 字段类型

https://symfony.com/doc/4.4/reference/forms/types/repeated.html

© www.soinside.com 2019 - 2024. All rights reserved.