Symfony 5 约束验证:自定义错误消息

我想使用SF 4.3上发布的新不承诺密码:https://symfony.com/blog/new-in-symfony-4-3-compromised-password-validator


我已经在我的验证.yaml上设置了它,如下所示:


App\Entity\User:

    constraints:

        - App\Validator\Constraints\ConstraintPassword: ~

    properties:

        plainPassword:

            - Symfony\Component\Validator\Constraints\NotCompromisedPassword: ~

它可以工作,但我想自定义错误消息,例如,直接在我的约束密码验证器上使用它.php:


<?php


namespace App\Validator\Constraints;


use App\Entity\User;

use Symfony\Component\Validator\Constraint;

use Symfony\Component\Validator\Constraints\NotCompromisedPassword;

use Symfony\Component\Validator\ConstraintValidator;


class ConstraintPasswordValidator extends ConstraintValidator

{

    /**

     * @param User $user

     * @param Constraint $constraint

     */

    public function validate($user, Constraint $constraint)

    {

        if (strlen($user->getPlainPassword()) < 8 || strlen($user->getPlainPassword() < 35)) {

            $this->context->buildViolation($constraint->lengthError)

                ->addViolation();

        }


        // Doing something like that

        $notCompromised = new NotCompromisedPassword();

        $notCompromised->message = "My custom error message";


       //Then, build the violation if password leaked

    }

}

也许它需要在我的约束密码中实例化和自定义.php?但我不知道如何


<?php


namespace App\Validator\Constraints;


use Symfony\Component\Validator\Constraint;


class ConstraintPassword extends Constraint

{

    public $lengthError = 'Erreur : La longueur du mot de passe doit être comprise entre 8 et 35 caractères';


    public function validatedBy()

    {

        return \get_class($this).'Validator';

    }


    public function getTargets()

    {

        return self::CLASS_CONSTRAINT;

    }

}


人到中年有点甜
浏览 124回答 1
1回答

犯罪嫌疑人X

您可以在验证上传递该选项。messageApp\Entity\User:&nbsp; &nbsp; properties:&nbsp; &nbsp; &nbsp; &nbsp; plainPassword:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - Symfony\Component\Validator\Constraints\NotCompromisedPassword:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message: "You error message"但是,如果要将约束验证为验证器,则可以使用:class MyValidator extends ConstraintValidator{&nbsp; &nbsp; public function validate($value, Constraint $chain)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Previous check...&nbsp; &nbsp; &nbsp; &nbsp; $groups = $this->context->getGroup();&nbsp; &nbsp; &nbsp; &nbsp; $violations = $this->context->getViolations();&nbsp; &nbsp; &nbsp; &nbsp; $current = $violations->count();&nbsp; &nbsp; &nbsp; &nbsp; // Execute the new constraint&nbsp; &nbsp; &nbsp; &nbsp; $this->context->getValidator()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->inContext($this->context)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->validate($value, new MyOtherConstraint(), $groups);&nbsp; &nbsp; &nbsp; &nbsp; // Check if the constraint has failed&nbsp; &nbsp; &nbsp; &nbsp; if ($violations->count() !== $current) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP