public function GenericMetadata::addConstraint in Plug 7
Adds a constraint.
If the constraint {@link Valid} is added, the cascading strategy will be changed to {@link CascadingStrategy::CASCADE}. Depending on the properties $traverse and $deep of that constraint, the traversal strategy will be set to one of the following:
- {@link TraversalStrategy::IMPLICIT} if $traverse is enabled and $deep is enabled
- {@link TraversalStrategy::IMPLICIT} | {@link TraversalStrategy::STOP_RECURSION} if $traverse is enabled, but $deep is disabled
- {@link TraversalStrategy::NONE} if $traverse is disabled
Parameters
Constraint $constraint The constraint to add:
Return value
GenericMetadata This object
Throws
ConstraintDefinitionException When trying to add the {@link Traverse} constraint
4 calls to GenericMetadata::addConstraint()
- ClassMetadata::addConstraint in lib/
Symfony/ validator/ Symfony/ Component/ Validator/ Mapping/ ClassMetadata.php - Adds a constraint.
- GenericMetadata::addConstraints in lib/
Symfony/ validator/ Symfony/ Component/ Validator/ Mapping/ GenericMetadata.php - Adds an list of constraints.
- GenericMetadata::__clone in lib/
Symfony/ validator/ Symfony/ Component/ Validator/ Mapping/ GenericMetadata.php - Clones this object.
- MemberMetadata::addConstraint in lib/
Symfony/ validator/ Symfony/ Component/ Validator/ Mapping/ MemberMetadata.php - Adds a constraint.
2 methods override GenericMetadata::addConstraint()
- ClassMetadata::addConstraint in lib/
Symfony/ validator/ Symfony/ Component/ Validator/ Mapping/ ClassMetadata.php - Adds a constraint.
- MemberMetadata::addConstraint in lib/
Symfony/ validator/ Symfony/ Component/ Validator/ Mapping/ MemberMetadata.php - Adds a constraint.
File
- lib/
Symfony/ validator/ Symfony/ Component/ Validator/ Mapping/ GenericMetadata.php, line 130
Class
- GenericMetadata
- A generic container of {@link Constraint} objects.
Namespace
Symfony\Component\Validator\MappingCode
public function addConstraint(Constraint $constraint) {
if ($constraint instanceof Traverse) {
throw new ConstraintDefinitionException(sprintf('The constraint "%s" can only be put on classes. Please use ' . '"Symfony\\Component\\Validator\\Constraints\\Valid" instead.', get_class($constraint)));
}
if ($constraint instanceof Valid) {
$this->cascadingStrategy = CascadingStrategy::CASCADE;
if ($constraint->traverse) {
// Traverse unless the value is not traversable
$this->traversalStrategy = TraversalStrategy::IMPLICIT;
if (!$constraint->deep) {
$this->traversalStrategy |= TraversalStrategy::STOP_RECURSION;
}
}
else {
$this->traversalStrategy = TraversalStrategy::NONE;
}
return $this;
}
$this->constraints[] = $constraint;
foreach ($constraint->groups as $group) {
$this->constraintsByGroup[$group][] = $constraint;
}
return $this;
}