class ConstraintViolationBuilder

Same name in this branch
  1. 10 core/lib/Drupal/Core/TypedData/Validation/ConstraintViolationBuilder.php \Drupal\Core\TypedData\Validation\ConstraintViolationBuilder
Same name in other branches
  1. 9 core/lib/Drupal/Core/TypedData/Validation/ConstraintViolationBuilder.php \Drupal\Core\TypedData\Validation\ConstraintViolationBuilder
  2. 8.9.x core/lib/Drupal/Core/TypedData/Validation/ConstraintViolationBuilder.php \Drupal\Core\TypedData\Validation\ConstraintViolationBuilder
  3. 11.x core/lib/Drupal/Core/TypedData/Validation/ConstraintViolationBuilder.php \Drupal\Core\TypedData\Validation\ConstraintViolationBuilder
  4. 11.x core/lib/Drupal/Core/Validation/ConstraintViolationBuilder.php \Drupal\Core\Validation\ConstraintViolationBuilder

A constraint violation builder for the basic Symfony validator.

We do not use the builder provided by Symfony as it is marked internal.

Hierarchy

  • class \Drupal\Core\Validation\ConstraintViolationBuilder implements \Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface

Expanded class hierarchy of ConstraintViolationBuilder

1 file declares its use of ConstraintViolationBuilder
ConstraintViolationBuilder.php in core/lib/Drupal/Core/TypedData/Validation/ConstraintViolationBuilder.php

File

core/lib/Drupal/Core/Validation/ConstraintViolationBuilder.php, line 20

Namespace

Drupal\Core\Validation
View source
class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface {
    
    /**
     * The number used
     */
    protected ?int $plural = NULL;
    
    /**
     * The code.
     */
    protected mixed $code = NULL;
    
    /**
     * The cause.
     */
    protected mixed $cause = NULL;
    
    /**
     * Constructs a new ConstraintViolationBuilder instance.
     *
     * @param \Symfony\Component\Validator\ConstraintViolationList $violations
     *   The violation list.
     * @param \Symfony\Component\Validator\Constraint $constraint
     *   The constraint.
     * @param string $message
     *   The message.
     * @param array $parameters
     *   The message parameters.
     * @param mixed $root
     *   The root.
     * @param string $propertyPath
     *   The property string.
     * @param mixed $invalidValue
     *   The invalid value.
     * @param \Drupal\Core\Validation\TranslatorInterface $translator
     *   The translator.
     * @param string|false|null $translationDomain
     *   (optional) The translation domain.
     */
    public function __construct(ConstraintViolationList $violations, Constraint $constraint, string $message, array $parameters, mixed $root, string $propertyPath, mixed $invalidValue, TranslatorInterface $translator, string|false|null $translationDomain = NULL) {
    }
    
    /**
     * {@inheritdoc}
     */
    public function atPath(mixed $path) : static {
        if (!is_string($path)) {
            @\trigger_error('Passing the $path parameter as a non-string value to ' . __METHOD__ . '() is deprecated in drupal:10.3.0 and will be required in drupal:11.0.0. See https://www.drupal.org/node/3396238', E_USER_DEPRECATED);
        }
        $this->propertyPath = PropertyPath::append($this->propertyPath, (string) $path);
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function setParameter(string $key, mixed $value) : static {
        $this->parameters[$key] = $value;
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function setParameters(array $parameters) : static {
        $this->parameters = $parameters;
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function setTranslationDomain(string $translationDomain) : static {
        $this->translationDomain = $translationDomain;
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function setInvalidValue(mixed $invalidValue) : static {
        $this->invalidValue = $invalidValue;
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function setPlural(int $number) : static {
        $this->plural = $number;
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function setCode(?string $code) : static {
        $this->code = $code;
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function setCause(mixed $cause) : static {
        $this->cause = $cause;
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function addViolation() : void {
        if (NULL === $this->plural) {
            $translatedMessage = $this->translator
                ->trans($this->message, $this->parameters, $this->translationDomain);
        }
        else {
            try {
                $translatedMessage = $this->translator
                    ->transChoice($this->message, $this->plural, $this->parameters, $this->translationDomain);
            } catch (\InvalidArgumentException $e) {
                $translatedMessage = $this->translator
                    ->trans($this->message, $this->parameters, $this->translationDomain);
            }
        }
        $this->violations
            ->add(new ConstraintViolation($translatedMessage, $this->message, $this->parameters, $this->root, $this->propertyPath, $this->invalidValue, $this->plural, $this->code, $this->constraint, $this->cause));
    }
    
    /**
     * {@inheritdoc}
     */
    public function disableTranslation() : static {
        $this->translationDomain = FALSE;
        return $this;
    }

}

Members


Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.