/** * @param mixed $subject * @param ConstraintValidatorFactoryInterface $constraintValidatorFactory * @param LegacyExecutionContextInterface|ExecutionContextInterface $context * @param string $group */ public function __construct($subject, ConstraintValidatorFactoryInterface $constraintValidatorFactory, $context, $group) { if (!$context instanceof LegacyExecutionContextInterface && !$context instanceof ExecutionContextInterface) { throw new \InvalidArgumentException(sprintf('Argument 3 passed to %s::__construct() must be an instance of Symfony\\Component\\Validator\\ExecutionContextInterface or Symfony\\Component\\Validator\\Context\\ExecutionContextInterface.', get_class($this))); } $this->subject = $subject; $this->context = $context; $this->group = $group; $this->constraintValidatorFactory = $constraintValidatorFactory; $this->current = ''; $this->basePropertyPath = $this->context->getPropertyPath(); }
/** * {@inheritdoc} */ public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null) { $classMetadata = $this->metadataFactory->getMetadataFor($objectOrClass); if (!$classMetadata instanceof ClassMetadataInterface) { // Cannot be UnsupportedMetadataException because of BC with // Symfony < 2.5 throw new ValidatorException(sprintf('The metadata factory should return instances of ' . '"\\Symfony\\Component\\Validator\\Mapping\\ClassMetadataInterface", ' . 'got: "%s".', is_object($classMetadata) ? get_class($classMetadata) : gettype($classMetadata))); } $propertyMetadatas = $classMetadata->getPropertyMetadata($propertyName); $groups = $groups ? $this->normalizeGroups($groups) : $this->defaultGroups; if (is_object($objectOrClass)) { $object = $objectOrClass; $cacheKey = spl_object_hash($objectOrClass); $propertyPath = PropertyPath::append($this->defaultPropertyPath, $propertyName); } else { // $objectOrClass contains a class name $object = null; $cacheKey = null; $propertyPath = $this->defaultPropertyPath; } $previousValue = $this->context->getValue(); $previousObject = $this->context->getObject(); $previousMetadata = $this->context->getMetadata(); $previousPath = $this->context->getPropertyPath(); $previousGroup = $this->context->getGroup(); foreach ($propertyMetadatas as $propertyMetadata) { $this->validateGenericNode($value, $object, $cacheKey . ':' . $propertyName, $propertyMetadata, $propertyPath, $groups, null, TraversalStrategy::IMPLICIT, $this->context); } $this->context->setNode($previousValue, $previousObject, $previousMetadata, $previousPath); $this->context->setGroup($previousGroup); return $this; }
/** * Validates a Typed Data node in the validation tree. * * If no constraints are passed, the data is validated against the * constraints specified in its data definition. If the data is complex or a * list and no constraints are passed, the contained properties or list items * are validated recursively. * * @param \Drupal\Core\TypedData\TypedDataInterface $data * The data to validated. * @param \Symfony\Component\Validator\Constraint[]|null $constraints * (optional) If set, an array of constraints to validate. * @param bool $is_root_call * (optional) Whether its the most upper call in the type data tree. * * @return $this */ protected function validateNode(TypedDataInterface $data, $constraints = NULL, $is_root_call = FALSE) { $previous_value = $this->context->getValue(); $previous_object = $this->context->getObject(); $previous_metadata = $this->context->getMetadata(); $previous_path = $this->context->getPropertyPath(); $metadata = $this->metadataFactory->getMetadataFor($data); $cache_key = spl_object_hash($data); $property_path = $is_root_call ? '' : PropertyPath::append($previous_path, $data->getName()); // Pass the canonical representation of the data as validated value to // constraint validators, such that they do not have to care about Typed // Data. $value = $this->typedDataManager->getCanonicalRepresentation($data); $this->context->setNode($value, $data, $metadata, $property_path); if (isset($constraints) || !$this->context->isGroupValidated($cache_key, Constraint::DEFAULT_GROUP)) { if (!isset($constraints)) { $this->context->markGroupAsValidated($cache_key, Constraint::DEFAULT_GROUP); $constraints = $metadata->findConstraints(Constraint::DEFAULT_GROUP); } $this->validateConstraints($value, $cache_key, $constraints); } // If the data is a list or complex data, validate the contained list items // or properties. However, do not recurse if the data is empty. if (($data instanceof ListInterface || $data instanceof ComplexDataInterface) && !$data->isEmpty()) { foreach ($data as $name => $property) { $this->validateNode($property); } } $this->context->setNode($previous_value, $previous_object, $previous_metadata, $previous_path); return $this; }
public function checkRequirementField($value, ExecutionContextInterface $context) { $data = $context->getRoot()->getData(); $type = $data["type"]; if (false !== strpos($context->getPropertyPath(), $type)) { // extract requirement type if (preg_match('@\\:(.+)\\]@', $context->getPropertyPath(), $matches)) { $requirementType = $matches[1]; if (isset(self::$typeList[$requirementType])) { /** @var TypeInterface $typeClass */ $typeClass = self::$typeList[$requirementType]; $typeClass->verifyForm($value, $context); return; } } $context->addViolation(Translator::getInstance()->trans("Impossible to check value `%value` for `%type` type", ['%value' => $value, '%type' => $type])); } }
function let(ProductRepositoryInterface $productRepository, UniqueValuesSet $uniqueValuesSet, ExecutionContextInterface $context, Form $form, ProductInterface $product, ProductValueInterface $value) { $this->beConstructedWith($productRepository, $uniqueValuesSet); $product->getValue('unique_attribute')->willReturn($value); $form->getData()->willReturn($product); $context->getPropertyPath()->willReturn(self::PROPERTY_PATH); $context->getRoot()->willReturn($form); $this->initialize($context); }
private function getRowData(ExecutionContextInterface $context) { $propertyPath = $context->getPropertyPath(); $data = $this->getRowData($context); }
/** * Custom validation constraint * Not valid if no one recipient specified * * @param ExecutionContextInterface $context */ public function isValid(ExecutionContextInterface $context) { $notValid = $this->getGroups()->isEmpty() && $this->getUsers()->isEmpty() && $this->getEmail() == null && $this->getOwner() == null; if ($notValid) { $propertyPath = $context->getPropertyPath() . '.recipientList'; $context->addViolationAt($propertyPath, 'oro.notification.validators.recipient_list.empty.message'); } }
public function isRegionValid(ExecutionContextInterface $context) { if ($this->getCountry() && $this->getCountry()->hasRegions() && !$this->region && !$this->regionText) { // do not allow saving text region in case when region was checked from list // except when in base data region text existed // another way region_text field will be null, logic are placed in form listener $propertyPath = $context->getPropertyPath() . '.region'; $context->addViolationAt($propertyPath, 'State is required for country {{ country }}', ['{{ country }}' => $this->getCountry()->getName()]); } }
/** * {@inheritdoc} */ public function atPath($path) { $this->defaultPropertyPath = $this->context->getPropertyPath($path); return $this; }