/** * Wrapper for {@link ExecutionContextInterface::buildViolation} that * supports the 2.4 context API. * * @param ExecutionContextInterface $context The context to use * @param string $message The violation message * @param array $parameters The message parameters * * @return ConstraintViolationBuilderInterface The violation builder * * @deprecated This method will be removed in Symfony 3.0. */ protected function buildViolationInContext(ExecutionContextInterface $context, $message, array $parameters = array()) { if ($context instanceof ExecutionContextInterface2Dot5) { return $context->buildViolation($message, $parameters); } return new LegacyConstraintViolationBuilder($context, $message, $parameters); }
public function validateSlug($blog, ExecutionContextInterface $context) { $unique = $this->getDoctrineRepo('AppBundle:Blog')->isSlugUnique($blog->getSlug(), $blog->getId()); if (!$unique) { $context->buildViolation('The slug is not unique')->addViolation(); } }
/** * Wrapper for {@link ExecutionContextInterface::buildViolation} that * supports the 2.4 context API. * * @param ExecutionContextInterface $context The context to use * @param string $message The violation message * @param array $parameters The message parameters * * @return ConstraintViolationBuilderInterface The violation builder * * @deprecated since version 2.5, to be removed in 3.0. */ protected function buildViolationInContext(ExecutionContextInterface $context, $message, array $parameters = array()) { @trigger_error('The ' . __METHOD__ . ' is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED); if ($context instanceof ExecutionContextInterface2Dot5) { return $context->buildViolation($message, $parameters); } return new LegacyConstraintViolationBuilder($context, $message, $parameters); }
/** * @param string|array $message * @param array $parameters * @param null $value * * @return ErrorElement */ public function addViolation($message, $parameters = array(), $value = null) { if (is_array($message)) { $value = isset($message[2]) ? $message[2] : $value; $parameters = isset($message[1]) ? (array) $message[1] : array(); $message = isset($message[0]) ? $message[0] : 'error'; } $subPath = (string) $this->getCurrentPropertyPath(); if ($this->context instanceof LegacyExecutionContextInterface) { $this->context->addViolationAt($subPath, $message, $parameters, $value); } else { $this->context->buildViolation($message)->atPath($subPath)->setParameters($parameters)->setInvalidValue($value)->addViolation(); } $this->errors[] = array($message, $parameters, $value); return $this; }
/** * @Assert\Callback * * @param ExecutionContextInterface $context */ public function validate(ExecutionContextInterface $context) { $ccn = $this->getCreditCardNumberPlain(); $ti = $this->typeInfo[$this->getType()]; if (!preg_match('/' . $ti[0] . '/', $ccn, $match)) { $context->buildViolation('Card number not matched with his type.')->atPath('creditCardNumberPlain')->addViolation(); } $isValidLength = false; $maxLengths = explode('|', $ti[1]); foreach ($maxLengths as $len) { if ($len == strlen($ccn)) { $isValidLength = true; break; } } if (!$isValidLength) { $context->buildViolation('Invalid card number length.')->atPath('creditCardNumberPlain')->addViolation(); } }
public function validateDiscountCode($data, ExecutionContextInterface $context) { if (empty($data['discountCode'])) { return; } $dcode = $this->getDoctrineRepo('AppBundle:DiscountCode')->findOneByCode($data['discountCode']); if ($dcode === null || $dcode->getStatus() != DiscountCode::STATUS_ASSIGNED) { $context->buildViolation('This is not a valid discount code')->atPath('discountCode')->addViolation(); return; } $inq = $this->getDoctrineRepo('AppBundle:TalentInquiry')->findOneByUuid($data['uuid']); $user = $inq->getUser(); if ($user === null || $user->getId() !== $dcode->getUser()->getId()) { $context->buildViolation('This is not a valid discount code')->atPath('discountCode')->addViolation(); } }
/** * @Assert\Callback */ public function validate(ExecutionContextInterface $context) { if ($this->title != ucfirst($this->title)) { $context->buildViolation("Le titre {{ value }} doit commencer en majuscule !")->atPath("title")->setParameter("{{ value }}", $this->title)->addViolation(); } }
/** * @Assert\Callback */ public function validate(ExecutionContextInterface $context) { if ($this->pseudo == "admin" && $this->password == "admin") { $context->buildViolation("Attention votre speudo ou mot de pass: {{ value }} n'est pas accepté")->atPath("pseudo")->setParameter("{{ value }}", $this->pseudo)->addViolation(); } elseif ($this->pseudo == "admin" || $this->password == "admin") { $context->buildViolation("Cet mot {{ value }} ne doit pas etre votre speudo!")->atPath("pseudo")->setParameter("{{ value }}", $this->pseudo)->addViolation(); } }
public function validateReason($talent, ExecutionContextInterface $context) { if ($this->formHelper != null) { $status = $this->formHelper['status']->getData(); $reason = $this->formHelper['reason']->getData(); if ($status == Talent::STATUS_REJECTED && ($reason == null || $reason == "")) { $context->buildViolation('You have to enter rejection reason.')->addViolation(); } } }
/** * @param ExecutionContextInterface|LegacyExecutionContextInterface $context */ public function isStatusErroneous($context) { if ($this->getBinaryContent() && $this->getProviderStatus() == self::STATUS_ERROR) { // Interface compatibility, the new ExecutionContextInterface should be typehinted when support for Symfony <2.5 is dropped if (!$context instanceof ExecutionContextInterface && !$context instanceof LegacyExecutionContextInterface) { throw new \InvalidArgumentException('Argument 1 should be an instance of Symfony\\Component\\Validator\\ExecutionContextInterface or Symfony\\Component\\Validator\\Context\\ExecutionContextInterface'); } if ($context instanceof LegacyExecutionContextInterface) { $context->addViolationAt('binaryContent', 'invalid', array(), null); } else { $context->buildViolation('invalid')->atPath('binaryContent')->addViolation(); } } }
/** * @Assert\Callback */ public function isChoixValid(ExecutionContextInterface $context) { // valide si les choix ne sont pas le même jour $aJours = array(); foreach ($this->choix as $c) { $aJours[] = $c->getActivite()->getJour(); } $aJours = array_unique($aJours); if (count($aJours) != count($this->choix)) { $context->buildViolation('choix.joursidentiques')->atPath('choix')->addViolation(); } }
/** * {@inheritdoc} */ public function isVoteValid(ExecutionContextInterface $context) { if (!$this->checkValue($this->value)) { $message = 'A vote cannot have a 0 value'; $propertyPath = $context->getPropertyPath() . '.value'; if ($context instanceof \Symfony\Component\Validator\Context\ExecutionContextInterface) { // Validator 2.5 API $context->buildViolation($message)->atPath($propertyPath)->addViolation(); } else { $context->addViolationAt($propertyPath, $message); } } }