Php Symfony验证器-哈希前检查密码长度和内容
我创建了一个密码验证,但是当我插入一个新用户时,这个验证会在加密密码后触发 我正在使用Argon2Id,因此密码将超过30个字符 我的验证器(config/validator/validation.yaml): 在usilizateur.php中设置MDP:Php Symfony验证器-哈希前检查密码长度和内容,php,symfony,validation,encryption,passwords,Php,Symfony,Validation,Encryption,Passwords,我创建了一个密码验证,但是当我插入一个新用户时,这个验证会在加密密码后触发 我正在使用Argon2Id,因此密码将超过30个字符 我的验证器(config/validator/validation.yaml): 在usilizateur.php中设置MDP: public function setMdp(string $mdp): self { $this->mdp = password_hash($mdp, PASSWORD_ARGON2ID);
public function setMdp(string $mdp): self
{
$this->mdp = password_hash($mdp, PASSWORD_ARGON2ID);
return $this;
}
因此,当我插入新用户时,Symfony validator告诉我密码超过30个字符,我收到了一个错误
如何在加密密码之前触发此验证程序
谢谢 你的方法不正确!您应该让set方法保持原样:
public function setMdp(string $mdp): self
{
$this->mdp = $mdp;
return $this;
}
相反,您可以在控制器中验证表单后对密码进行编码,如下所示:
public function createAccount(Request $request, UserPasswordEncoderInterface $passwordEncoder) {
...
$createAccountForm = $this->createForm(CreateAccountFormType::class);
$createAccountForm->handleRequest($request);
if ($createAccountForm->isSubmitted() && $createAccountForm->isValid()) {
$user = $createAccountForm->getData();
$user->setPassword($passwordEncoder->encodePassword($user, $user->getPlainPassword()));
// flush, persist and other changes
}
}
但是,正如您在我的示例中看到的,我正在编码$user->getPlainPassword()
,它是未映射(to-doctor)实体属性的getter,这是该属性的唯一作业
public function createAccount(Request $request, UserPasswordEncoderInterface $passwordEncoder) {
...
$createAccountForm = $this->createForm(CreateAccountFormType::class);
$createAccountForm->handleRequest($request);
if ($createAccountForm->isSubmitted() && $createAccountForm->isValid()) {
$user = $createAccountForm->getData();
$user->setPassword($passwordEncoder->encodePassword($user, $user->getPlainPassword()));
// flush, persist and other changes
}
}