Bonjour,
Voila je rencontre un petit problème avec mon code.
j'ai créé un service validator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| class CodeValidator extends ConstraintValidator
{
private $requestStack;
private $em;
public function __construct(RequestStack $requestStack, EntityManagerInterface $em)
{
$this->requestStack = $requestStack;
$this->em = $em;
}
/**
* Checks if the passed value is valid.
*
* @param mixed $value The value that should be validated
* @param Constraint $constraint The constraint for the validation
*/
public function validate($value, Constraint $constraint)
{
$request = $this->requestStack->getCurrentRequest();
$token = $request->attributes->get('token');
$user = $this->em
->getRepository('UserBundle:User')
->findOneBy(array('confirmationToken'=>$token));
$code = $user->getPasswordTmp();
if($value != $code)
$this->context->buildViolation($constraint->message)
->addViolation();
}
} |
Dans mon contrôleur je passe a validate les paramètre
voici le code de la contante:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| use Symfony\Component\Validator\Constraint;
class Code extends Constraint
{
public $message = "Votre code n'est pas valide.";
public function validatedBy()
{
return get_class($this).'Validator';
}
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
} |
voici se que j'ai m'y pour le service:
1 2 3 4 5
| app.form.validator.code:
class: UserBundle\Services\Validator\CodeValidator
arguments: [@request_stack, @doctrine.orm.entity_manager]
tags:
- { name: validator.constraint_validator, alias: app_form_validator_code } |
et sa me retourne:
Fatal error: Call to a member function buildViolation() on null
Partager