Home » Php » php – Symfony 5 Constraint Validation : Customize error message

php – Symfony 5 Constraint Validation : Customize error message

Posted by: admin February 25, 2020 Leave a comment

Questions:

I want to use the new NotCompromisedPassword released on SF 4.3 :
https://symfony.com/blog/new-in-symfony-4-3-compromised-password-validator

I’ve set it up on my validation.yaml like this :

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

It works, but i want to customize the error message, for example, by using it directly on my 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
    }
}

Maybe it needs to be instantiated and customized in my ConstraintPassword.php ? But i don’t know how

<?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;
    }
}
How to&Answers:

You can pass the message option on the validation.yaml

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

But if you want to validate a constraint into a validator, you can use :

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;
        }
    }
}