コード例 #1
0
 /**
  * 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);
 }
コード例 #2
0
 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();
     }
 }
コード例 #3
0
 /**
  * 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);
 }
コード例 #4
0
ファイル: ErrorElement.php プロジェクト: LamaDelRay/test_symf
 /**
  * @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;
 }
コード例 #5
0
 /**
  * @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();
     }
 }
コード例 #6
0
 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();
     }
 }
コード例 #7
0
ファイル: Category.php プロジェクト: abdcam/symfony3wa
 /**
  * @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();
     }
 }
コード例 #8
0
ファイル: User.php プロジェクト: abdcam/symfony3wa
 /**
  * @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();
     }
 }
コード例 #9
0
 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();
         }
     }
 }
コード例 #10
0
ファイル: Media.php プロジェクト: sidz/SonataMediaBundle
 /**
  * @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();
         }
     }
 }
コード例 #11
0
ファイル: Eleve.php プロジェクト: lfiduras/periscolaire
 /**
  * @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();
     }
 }
コード例 #12
0
ファイル: Vote.php プロジェクト: NonoXyde/FOSCommentBundle
 /**
  * {@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);
         }
     }
 }