i'm writing forgotten password form and, using php validation (as opposed yml), trying first of check whether e-mail address exists.
i think i'm there can't seem run doctrine query inside validator. here have far:
class emailexistsvalidator extends constraintvalidator { public function isvalid($value, constraint $constraint) { $em = $this->container->get('doctrine.orm.entity_manager'); $query = $em->createquery('select count(id) users email=:email') ->setparameter('email', $value); $count = $query->getquery()->getsinglescalarresult(); if ($count != 1) { $this->setmessage($constraint->message); return false; } return true; } } i "undefined property - container".
just pass doctrine service validator this:
protected $doctrine; public function __construct($doctrine) { $this->doctrine = $doctrine; } and in services.yml:
my_service.my_constraint: class: mycompany\mybundlebundle\validator\constraints\myvalidator arguments: - '@doctrine' tags: - { name: validator.constraint_validator, alias: my_name } and can in validator like: $this->doctrine->getrepository('...');
Comments
Post a Comment