Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ConstraintViolationBuilder.php

class ConstraintViolationBuilder

Same name in this branch
  1. 11.1.x core/lib/Drupal/Core/Validation/ConstraintViolationBuilder.php \Drupal\Core\Validation\ConstraintViolationBuilder

Default implementation of {@link ConstraintViolationBuilderInterface}.

@author Bernhard Schussek <bschussek@gmail.com>

@internal

Hierarchy

  • class \Symfony\Component\Validator\Violation\ConstraintViolationBuilder implements \Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface

Expanded class hierarchy of ConstraintViolationBuilder

1 file declares its use of ConstraintViolationBuilder
ExecutionContext.php in vendor/symfony/validator/Context/ExecutionContext.php

File

vendor/symfony/validator/Violation/ConstraintViolationBuilder.php, line 27

Namespace

Symfony\Component\Validator\Violation
View source
class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface {
    private string $propertyPath;
    private ?int $plural = null;
    private ?string $code = null;
    private mixed $cause = null;
    public function __construct(ConstraintViolationList $violations, ?Constraint $constraint, string|\Stringable $message, array $parameters, mixed $root, ?string $propertyPath, mixed $invalidValue, TranslatorInterface $translator, string|false|null $translationDomain = null) {
        $this->propertyPath = $propertyPath ?? '';
    }
    public function atPath(string $path) : static {
        $this->propertyPath = PropertyPath::append($this->propertyPath, $path);
        return $this;
    }
    public function setParameter(string $key, string $value) : static {
        $this->parameters[$key] = $value;
        return $this;
    }
    public function setParameters(array $parameters) : static {
        $this->parameters = $parameters;
        return $this;
    }
    public function setTranslationDomain(string $translationDomain) : static {
        $this->translationDomain = $translationDomain;
        return $this;
    }
    
    /**
     * @return $this
     */
    public function disableTranslation() : static {
        $this->translationDomain = false;
        return $this;
    }
    public function setInvalidValue(mixed $invalidValue) : static {
        $this->invalidValue = $invalidValue;
        return $this;
    }
    public function setPlural(int $number) : static {
        $this->plural = $number;
        return $this;
    }
    public function setCode(?string $code) : static {
        $this->code = $code;
        return $this;
    }
    public function setCause(mixed $cause) : static {
        $this->cause = $cause;
        return $this;
    }
    public function addViolation() : void {
        $parameters = null === $this->plural ? $this->parameters : [
            '%count%' => $this->plural,
        ] + $this->parameters;
        if (false === $this->translationDomain) {
            $translatedMessage = strtr($this->message, $parameters);
        }
        else {
            $translatedMessage = $this->translator
                ->trans($this->message, $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));
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ConstraintViolationBuilder::$cause private property
ConstraintViolationBuilder::$code private property
ConstraintViolationBuilder::$plural private property
ConstraintViolationBuilder::$propertyPath private property
ConstraintViolationBuilder::addViolation public function Adds the violation to the current execution context. Overrides ConstraintViolationBuilderInterface::addViolation
ConstraintViolationBuilder::atPath public function Stores the property path at which the violation should be generated. Overrides ConstraintViolationBuilderInterface::atPath
ConstraintViolationBuilder::disableTranslation public function Overrides ConstraintViolationBuilderInterface::disableTranslation
ConstraintViolationBuilder::setCause public function Sets the cause of the violation. Overrides ConstraintViolationBuilderInterface::setCause
ConstraintViolationBuilder::setCode public function Sets the violation code. Overrides ConstraintViolationBuilderInterface::setCode
ConstraintViolationBuilder::setInvalidValue public function Sets the invalid value that caused this violation. Overrides ConstraintViolationBuilderInterface::setInvalidValue
ConstraintViolationBuilder::setParameter public function Sets a parameter to be inserted into the violation message. Overrides ConstraintViolationBuilderInterface::setParameter
ConstraintViolationBuilder::setParameters public function Sets all parameters to be inserted into the violation message. Overrides ConstraintViolationBuilderInterface::setParameters
ConstraintViolationBuilder::setPlural public function Sets the number which determines how the plural form of the violation
message is chosen when it is translated.
Overrides ConstraintViolationBuilderInterface::setPlural
ConstraintViolationBuilder::setTranslationDomain public function Sets the translation domain which should be used for translating the
violation message.
Overrides ConstraintViolationBuilderInterface::setTranslationDomain
ConstraintViolationBuilder::__construct public function

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal