【问题标题】:Symfony 5 Constraint Validation : Customize error messageSymfony 5 约束验证:自定义错误消息
【发布时间】:2020-02-12 11:07:25
【问题描述】:

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

我已经在我的validation.yaml 上这样设置了:

App\Entity\User:
    constraints:
        - App\Validator\Constraints\ConstraintPassword: ~
    properties:
        plainPassword:
            - Symfony\Component\Validator\Constraints\NotCompromisedPassword: ~

它有效,但我想自定义错误消息,例如,直接在我的 ConstraintPasswordValidator.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
    }
}

也许它需要在我的 ConstraintPassword.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;
    }
}

【问题讨论】:

    标签: php symfony validation


    【解决方案1】:

    您可以在validation.yaml 中传递message 选项

    App\Entity\User:
        properties:
            plainPassword:
                - Symfony\Component\Validator\Constraints\NotCompromisedPassword:
                    message: "You error message"
    

    但是如果你想把一个约束验证成一个验证器,你可以使用:

    class MyValidator extends ConstraintValidator
    {
        public function validate($value, Constraint $chain)
        {
            // Previous check...
    
            $groups = $this->context->getGroup();
            $violations = $this->context->getViolations();
            $current = $violations->count();
    
            // Execute the new constraint
            $this->context->getValidator()
                ->inContext($this->context)
                ->validate($value, new MyOtherConstraint(), $groups);
    
            // Check if the constraint has failed
            if ($violations->count() !== $current) {
                return;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-04-09
      • 2011-11-27
      • 2021-04-11
      • 2015-09-07
      • 2016-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多