/**
  * @param JsonSerializationVisitor     $visitor
  * @param ConstraintViolationInterface $violation
  * @return array
  */
 public function serializeToJson(JsonSerializationVisitor $visitor, ConstraintViolationInterface $violation)
 {
     $data = array('propertyPath' => $this->transformPropertyPath($violation->getPropertyPath()), 'message' => $violation->getMessage(), 'code' => $violation->getCode(), 'value' => $violation->getInvalidValue());
     if (null === $visitor->getRoot()) {
         $visitor->setRoot($data);
     }
     return $data;
 }
 /**
  * {@inheritdoc}
  */
 public function format(Param $param, ConstraintViolationInterface $violation)
 {
     return sprintf("%s parameter %s value '%s' violated a constraint (%s)", $param instanceof QueryParam ? 'Query' : 'Request', $param->getKey(), $violation->getInvalidValue(), $violation->getMessage());
 }
 /**
  * @param Container $container
  * @param ConstraintViolationInterface $violation
  * @return Nette\Forms\IControl|Nette\Forms\Controls\BaseControl|UI\Form
  */
 private function findControl(Container $container, ConstraintViolationInterface $violation)
 {
     if (!($m = Nette\Utils\Strings::split('.' . $violation->getPropertyPath(), '~([\\.\\[])~'))) {
         return $container->getForm();
         // apply the error to form
     }
     $control = $container;
     while (($type = array_shift($m)) !== NULL && $control) {
         if (empty($type)) {
             continue;
         }
         $step = array_shift($m);
         if ($type === '[') {
             $step = substr($step, 0, -1);
         }
         $control = $control->getComponent($step, FALSE);
     }
     return $control instanceof Nette\Forms\IControl ? $control : $container->getForm();
 }
Example #4
0
 /**
  * @param ConstraintViolationInterface|ConstraintViolation $error
  * @return array
  */
 private function getValidationErrorArray($error)
 {
     return ['i18nKey' => method_exists($error->getConstraint(), 'getI18nKey') ? $error->getConstraint()->getI18nKey() : get_class($error->getConstraint()), 'messageTemplate' => $error->getMessageTemplate(), 'plural' => $error->getPlural(), 'invalidValue' => $error->getInvalidValue(), 'parameters' => $error->getParameters(), 'message' => $error->getMessage(), 'path' => $error->getPropertyPath()];
 }
 /**
  * Returns an error array for a constraint violation
  *
  * @param ConstraintViolationInterface $violation
  *
  * @return array
  */
 protected function getViolationError(ConstraintViolationInterface $violation)
 {
     return array($violation->getMessageTemplate(), $violation->getMessageParameters());
 }
 /**
  * Given a constraint violation, return its property path as an array
  *
  * Example:
  *     Property path of violation: "[foo][0][bar][baz]"
  *     Return value: array('foo', '0', 'bar', 'baz')
  *
  * @param  ConstraintViolationInterface $violation Violation whose path to return
  * @return array
  */
 protected function getPropertyPathAsArray(ConstraintViolationInterface $violation)
 {
     $path = $violation->getPropertyPath();
     // Remove outer brackets and explode fields into array
     $path = substr($path, 1, strlen($path) - 2);
     return explode('][', $path);
 }
 public function __construct(ConstraintViolationInterface $violation)
 {
     $this->propertyPath = $violation->getPropertyPath();
     $this->message = $violation->getMessage();
 }
 function it_throws_an_exception_if_product_argument_is_not_a_product(ConstraintViolationInterface $violation)
 {
     $violation->getPropertyPath()->willReturn('values[price].float');
     $this->shouldThrow(new \InvalidArgumentException('Expects a Pim\\Component\\Catalog\\Model\\ProductInterface'))->duringNormalize($violation, 'internal_api', ['product' => new ProductValue()]);
 }
 public function handle(ConstraintViolationInterface $violation, $violationTarget)
 {
     $violationTarget->addError($violation->getMessage());
 }
 /**
  * {@inheritdoc}
  */
 public function format(ParamInterface $param, ConstraintViolationInterface $violation)
 {
     return sprintf("Parameter %s value '%s' violated a constraint (%s)", $param->getName(), $violation->getInvalidValue(), $violation->getMessage());
 }
Example #11
0
 /**
  * @param ConstraintViolationInterface $violation
  */
 public function violation(ConstraintViolationInterface $violation)
 {
     $this->violations[] = $violation;
     $this->logger->warning("DataTrans: validation warning, '{class}', '{property}', '{message}', '{invalidValue}'!", array('content' => get_class($violation->getRoot()), 'property' => $violation->getPropertyPath(), 'message' => $violation->getMessage(), 'invalidValue' => is_scalar($violation->getInvalidValue()) ? $violation->getInvalidValue() : '_object_'));
 }
 /**
  * Checks if the given violation is the appropriate one.
  *
  * @param ConstraintViolationInterface $violation
  *
  * @return bool
  */
 private function isUniquePropertyViolation(ConstraintViolationInterface $violation) : bool
 {
     return UniqueProperty::NON_UNIQUE_PROPERTY === $violation->getCode() && 'username' === $violation->getPropertyPath();
     // the username property is always `username` in the `CreateUserDTO`
 }
Example #13
0
 /**
  * @param Container $container
  * @param ConstraintViolationInterface $violation
  * @return Nette\Forms\IControl|Nette\Forms\Controls\BaseControl|Nette\Application\UI\Form
  */
 protected function findControl(Container $container, ConstraintViolationInterface $violation)
 {
     // propertyPath eg. addresses[1].street
     $path = preg_split('/\\]\\.|\\.|\\[|\\]/', $violation->getPropertyPath(), -1, PREG_SPLIT_NO_EMPTY);
     $control = $container;
     while (($step = array_shift($path)) !== NULL && $control) {
         $control = $control->getComponent($step, FALSE);
     }
     return $control instanceof Nette\Forms\IControl || $control instanceof IContainer ? $control : $container->getForm();
 }
 public function throwValidationException(ConstraintViolationInterface $violation)
 {
     $exception_message = $violation->getMessage();
     throw new ValidationException($exception_message);
 }
 /**
  * @Serializer\Groups({"all", "debug"})
  * @Serializer\SerializedName("invalidValue")
  * @Serializer\VirtualProperty
  */
 public function getCode()
 {
     return $this->constraintViolation->getCode();
 }