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

Breadcrumb

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

function ClassMetadata::addConstraint

If the constraint {@link Cascade} is added, the cascading strategy will be changed to {@link CascadingStrategy::CASCADE}.

If the constraint {@link Traverse} is added, the traversal strategy will be changed. Depending on the $traverse property of that constraint, the traversal strategy will be set to one of the following:

  • {@link TraversalStrategy::IMPLICIT} by default
  • {@link TraversalStrategy::NONE} if $traverse is disabled
  • {@link TraversalStrategy::TRAVERSE} if $traverse is enabled

Overrides GenericMetadata::addConstraint

1 call to ClassMetadata::addConstraint()
ClassMetadata::mergeConstraints in vendor/symfony/validator/Mapping/ClassMetadata.php
Merges the constraints of the given metadata into this object.

File

vendor/symfony/validator/Mapping/ClassMetadata.php, line 176

Class

ClassMetadata
Default implementation of {@link ClassMetadataInterface}.

Namespace

Symfony\Component\Validator\Mapping

Code

public function addConstraint(Constraint $constraint) : static {
    $this->checkConstraint($constraint);
    if ($constraint instanceof Traverse) {
        if ($constraint->traverse) {
            // If traverse is true, traversal should be explicitly enabled
            $this->traversalStrategy = TraversalStrategy::TRAVERSE;
        }
        else {
            // If traverse is false, traversal should be explicitly disabled
            $this->traversalStrategy = TraversalStrategy::NONE;
        }
        // The constraint is not added
        return $this;
    }
    if ($constraint instanceof Cascade) {
        $this->cascadingStrategy = CascadingStrategy::CASCADE;
        foreach ($this->getReflectionClass()
            ->getProperties() as $property) {
            if (isset($constraint->exclude[$property->getName()])) {
                continue;
            }
            if ($this->canCascade($property->getType())) {
                $this->addPropertyConstraint($property->getName(), new Valid());
            }
        }
        // The constraint is not added
        return $this;
    }
    $constraint->addImplicitGroupName($this->getDefaultGroup());
    parent::addConstraint($constraint);
    return $this;
}

API Navigation

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