Author: Bernhard Schussek (bschussek@gmail.com)
Inheritance: extends Symfony\Component\Validator\ConstraintValidator
示例#1
0
 /**
  * @dataProvider validateDataProvider
  * @param mixed $data
  * @param boolean $correct
  */
 public function testValidate($data, $correct)
 {
     if (!$correct) {
         $this->context->expects($this->once())->method('addViolation')->with($this->constraint->message);
     } else {
         $this->context->expects($this->never())->method('addViolation');
     }
     $this->validator->validate($data, $this->constraint);
 }
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     parent::validate($value, $constraint);
     $host = substr($value, strpos($value, '@') + 1);
     if (in_array($host, $this->blacklist)) {
         $this->context->addViolation($constraint->message, ['%host%' => $host]);
     }
 }
 /**
  * {@inheritdoc}
  *
  * @param string|array $value
  * @param Constraint $constraint
  */
 public function validate($value, Constraint $constraint)
 {
     if (is_scalar($value)) {
         $value = [$value];
     }
     $emailValidator = new EmailValidator();
     $emailValidator->initialize($this->context);
     foreach ($value as $fullEmailAddress) {
         $email = $this->emailAddressHelper->extractPureEmailAddress($fullEmailAddress);
         if (!$email) {
             continue;
         }
         $emailValidator->validate($email, $constraint);
         if ($this->context->getViolations()->count()) {
             foreach ($this->context->getViolations() as $violation) {
                 /** @var ConstraintViolation $violation */
                 if ($violation->getPropertyPath() == $this->context->getPropertyPath()) {
                     return;
                 }
             }
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function validate($entity, Constraint $constraint)
 {
     if (!$constraint instanceof Email) {
         throw new \Exception('This constraint must be instance of EcentriaRestBundle:Email');
     }
     $method = $constraint->dependencyGetter;
     $getter = $constraint->emailGetter;
     if (method_exists($entity, $method) && method_exists($entity, $getter)) {
         if ($entity->{$method}() === $constraint->dependencyMatch) {
             parent::validate($entity->{$getter}(), $constraint);
             $violations = $this->context->getViolations();
             foreach ($violations as $key => $violation) {
                 /** @var ConstraintViolation $violation */
                 $constraintViolation = new ConstraintViolation($violation->getMessage(), $violation->getMessageTemplate(), $violation->getParameters(), $violation->getRoot(), $constraint->propertyPath, $violation->getInvalidValue(), $violation->getPlural(), $violation->getCode());
                 $violations->set($key, $constraintViolation);
             }
         }
     }
 }
 public function validate($value, Constraint $constraint)
 {
     $currentViolationsCount = \count($this->context->getViolations());
     $currentMessage = $constraint->message;
     $constraint->message = $constraint->invalidEmailMessage;
     // switch first to invalidEmailMessage
     // do validation of EmailValidator
     parent::validate($value, $constraint);
     // there was violation in EmailValidator
     if (\count($this->context->getViolations()) > $currentViolationsCount) {
         return;
     }
     $constraint->message = $currentMessage;
     // switch to original constraint message
     if ($constraint->currentAccountEmail != $value) {
         // if email set is same as the current email do nothing
         $user = $this->service->find(array('email' => $value), array());
         if (!$user) {
             return;
         }
         $this->context->addViolation($constraint->message, array('{{ field }}' => $value));
     }
     return;
 }