getPropertyPath() публичный Метод

public getPropertyPath ( )
Пример #1
0
 private function getLineNumber(ConstraintViolation $violation)
 {
     if (!preg_match('/lines\\[(\\d+)\\]/', $violation->getPropertyPath(), $matches)) {
         throw new \InvalidArgumentException(sprintf('Cannot get line number of string "%s". Format has to be: line[%line_number%]', $violation->getPropertyPath()));
     }
     return intval($matches[1]);
 }
 protected function getConstraintViolation(ConstraintViolation $constraintViolation, array &$messages)
 {
     $path = new PropertyPath($constraintViolation->getPropertyPath());
     $elements = $path->getElements();
     $elements[] = $constraintViolation->getConstraint()->validatedBy();
     $this->propertyAccessor->setValue($messages, $this->buildPath($elements), $constraintViolation->getMessage());
 }
 public function serializeViolation(LuaSerializationVisitor $visitor, ConstraintViolation $violation, array $type = null)
 {
     $data = ['property_path' => $violation->getPropertyPath(), 'message' => $violation->getMessage()];
     if (null === $visitor->getRoot()) {
         $visitor->setRoot($data);
     }
     return $data;
 }
 /**
  * @param ConstraintViolation $constraint constraint
  *
  * @return self
  */
 public static function createFromConstraintViolation(ConstraintViolation $constraint)
 {
     $instance = new static();
     $instance->setCode($constraint->getCode());
     $instance->setMessage($constraint->getMessageTemplate());
     $instance->setMessageParameters($constraint->getParameters());
     $instance->setSubjectProperty($constraint->getPropertyPath());
     $instance->setCreatedAt(new \DateTime());
     return $instance;
 }
Пример #5
0
 /**
  * {@inheritdoc}
  */
 public function mapViolation(ConstraintViolation $violation, FormInterface $form, $allowNonSynchronized = false)
 {
     $this->allowNonSynchronized = $allowNonSynchronized;
     // The scope is the currently found most specific form that
     // an error should be mapped to. After setting the scope, the
     // mapper will try to continue to find more specific matches in
     // the children of scope. If it cannot, the error will be
     // mapped to this scope.
     $scope = null;
     $violationPath = null;
     $relativePath = null;
     $match = false;
     // Don't create a ViolationPath instance for empty property paths
     if (strlen($violation->getPropertyPath()) > 0) {
         $violationPath = new ViolationPath($violation->getPropertyPath());
         $relativePath = $this->reconstructPath($violationPath, $form);
     }
     // This case happens if the violation path is empty and thus
     // the violation should be mapped to the root form
     if (null === $violationPath) {
         $scope = $form;
     }
     // In general, mapping happens from the root form to the leaf forms
     // First, the rules of the root form are applied to determine
     // the subsequent descendant. The rules of this descendant are then
     // applied to find the next and so on, until we have found the
     // most specific form that matches the violation.
     // If any of the forms found in this process is not synchronized,
     // mapping is aborted. Non-synchronized forms could not reverse
     // transform the value entered by the user, thus any further violations
     // caused by the (invalid) reverse transformed value should be
     // ignored.
     if (null !== $relativePath) {
         // Set the scope to the root of the relative path
         // This root will usually be $form. If the path contains
         // an unmapped form though, the last unmapped form found
         // will be the root of the path.
         $scope = $relativePath->getRoot();
         $it = new PropertyPathIterator($relativePath);
         while ($this->acceptsErrors($scope) && null !== ($child = $this->matchChild($scope, $it))) {
             $scope = $child;
             $it->next();
             $match = true;
         }
     }
     // This case happens if an error happened in the data under a
     // form inheriting its parent data that does not match any of the
     // children of that form.
     if (null !== $violationPath && !$match) {
         // If we could not map the error to anything more specific
         // than the root element, map it to the innermost directly
         // mapped form of the violation path
         // e.g. "children[foo].children[bar].data.baz"
         // Here the innermost directly mapped child is "bar"
         $scope = $form;
         $it = new ViolationPathIterator($violationPath);
         // Note: acceptsErrors() will always return true for forms inheriting
         // their parent data, because these forms can never be non-synchronized
         // (they don't do any data transformation on their own)
         while ($this->acceptsErrors($scope) && $it->valid() && $it->mapsForm()) {
             if (!$scope->has($it->current())) {
                 // Break if we find a reference to a non-existing child
                 break;
             }
             $scope = $scope->get($it->current());
             $it->next();
         }
     }
     // Follow dot rules until we have the final target
     $mapping = $scope->getConfig()->getOption('error_mapping');
     while ($this->acceptsErrors($scope) && isset($mapping['.'])) {
         $dotRule = new MappingRule($scope, '.', $mapping['.']);
         $scope = $dotRule->getTarget();
         $mapping = $scope->getConfig()->getOption('error_mapping');
     }
     // Only add the error if the form is synchronized
     if ($this->acceptsErrors($scope)) {
         $scope->addError(new FormError($violation->getMessage(), $violation->getMessageTemplate(), $violation->getMessageParameters(), $violation->getMessagePluralization()));
     }
 }
Пример #6
0
 /**
  * Creates a JSON error response from a ConstraintViolation
  */
 protected function createValidationErrorResponse(ConstraintViolation $violation)
 {
     return $this->createErrorResponse("The '" . $violation->getPropertyPath() . "' value is invalid.", $violation->getMessage());
 }
Пример #7
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()];
 }
 public function serializeViolationToYml(YamlSerializationVisitor $visitor, ConstraintViolation $violation, array $type = null)
 {
     return array('property_path' => $violation->getPropertyPath(), 'message' => $violation->getMessage());
 }
Пример #9
0
    /**
     * {@inheritdoc}
     */
    public function mapViolation(ConstraintViolation $violation, FormInterface $form, $allowNonSynchronized = false)
    {
        $this->allowNonSynchronized = $allowNonSynchronized;

        $violationPath = new ViolationPath($violation->getPropertyPath());
        $relativePath = $this->reconstructPath($violationPath, $form);
        $match = false;

        // In general, mapping happens from the root form to the leaf forms
        // First, the rules of the root form are applied to determine
        // the subsequent descendant. The rules of this descendant are then
        // applied to find the next and so on, until we have found the
        // most specific form that matches the violation.

        // If any of the forms found in this process is not synchronized,
        // mapping is aborted. Non-synchronized forms could not reverse
        // transform the value entered by the user, thus any further violations
        // caused by the (invalid) reverse transformed value should be
        // ignored.

        if (null !== $relativePath) {
            // Set the scope to the root of the relative path
            // This root will usually be $form. If the path contains
            // an unmapped form though, the last unmapped form found
            // will be the root of the path.
            $this->setScope($relativePath->getRoot());
            $it = new PropertyPathIterator($relativePath);

            while ($this->isValidScope() && null !== ($child = $this->matchChild($it))) {
                $this->setScope($child);
                $it->next();
                $match = true;
            }
        }

        // This case happens if an error happened in the data under a
        // virtual form that does not match any of the children of
        // the virtual form.
        if (!$match) {
            // If we could not map the error to anything more specific
            // than the root element, map it to the innermost directly
            // mapped form of the violation path
            // e.g. "children[foo].children[bar].data.baz"
            // Here the innermost directly mapped child is "bar"

            $it = new ViolationPathIterator($violationPath);
            // The overhead of setScope() is not needed anymore here
            $this->scope = $form;

            while ($this->isValidScope() && $it->valid() && $it->mapsForm()) {
                if (!$this->scope->has($it->current())) {
                    // Break if we find a reference to a non-existing child
                    break;
                }

                $this->scope = $this->scope->get($it->current());
                $it->next();
            }
        }

        // Follow dot rules until we have the final target
        $mapping = $this->scope->getConfig()->getOption('error_mapping');

        while ($this->isValidScope() && isset($mapping['.'])) {
            $dotRule = new MappingRule($this->scope, '.', $mapping['.']);
            $this->scope = $dotRule->getTarget();
            $mapping = $this->scope->getConfig()->getOption('error_mapping');
        }

        // Only add the error if the form is synchronized
        if ($this->isValidScope()) {
            $this->scope->addError(new FormError(
                $violation->getMessageTemplate(),
                $violation->getMessageParameters(),
                $violation->getMessagePluralization()
            ));
        }
    }