Author: Fabien Potencier (fabien@symfony.com)
Author: Florian Eckerstorfer (florian@eckerstorfer.org)
Author: Bernhard Schussek (bschussek@gmail.com)
Inheritance: extends Symfony\Component\Validator\ConstraintValidator
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     $typed_data = $this->context->getMetadata()->getTypedData();
     if ($typed_data instanceof AllowedValuesInterface) {
         $account = \Drupal::currentUser();
         $allowed_values = $typed_data->getSettableValues($account);
         $constraint->choices = $allowed_values;
         // If the data is complex, we have to validate its main property.
         if ($typed_data instanceof ComplexDataInterface) {
             $name = $typed_data->getDataDefinition()->getMainPropertyName();
             if (!isset($name)) {
                 throw new \LogicException('Cannot validate allowed values for complex data without a main property.');
             }
             $value = $typed_data->get($name)->getValue();
         }
     }
     // The parent implementation ignores values that are not set, but makes
     // sure some choices are available firstly. However, we want to support
     // empty choices for undefined values, e.g. if a term reference field
     // points to an empty vocabulary.
     if (!isset($value)) {
         return;
     }
     parent::validate($value, $constraint);
 }
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     $channels = $this->manager->getChannelChoices();
     if (0 === count($channels)) {
         throw new ConstraintDefinitionException('No channel is set in the application');
     }
     $constraint->choices = array_keys($channels);
     parent::validate($value, $constraint);
 }
 /**
  * Checks if the passed value is valid
  *
  * @param mixed      $value      The value that should be validated
  * @param Constraint $constraint The constraint for the validation
  *
  * @throws ConstraintDefinitionException
  *
  * @return void
  */
 public function validate($value, Constraint $constraint)
 {
     if (!$constraint->entity) {
         throw new ConstraintDefinitionException('Entity not specified');
     }
     $entity = $constraint->entity;
     $constraint->choices = $entity::getValues();
     return parent::validate($value, $constraint);
 }
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     $channels = $this->channelRepository->getLabelsIndexedByCode();
     if (0 === count($channels)) {
         throw new ConstraintDefinitionException('No channel is set in the application');
     }
     $constraint->choices = array_keys($channels);
     parent::validate($value, $constraint);
 }
 /**
  * Checks if the passed value is valid
  *
  * @param mixed      $value      The value that should be validated
  * @param Constraint $constraint The constraint for the validation
  *
  * @throws ConstraintDefinitionException
  *
  * @return void
  */
 public function validate($value, Constraint $constraint)
 {
     /** @var SetType $constraint */
     if (!$constraint->class) {
         throw new ConstraintDefinitionException('Target is not specified');
     }
     /** @var string $class class name of inheriting \Okapon\DoctrineSetTypeBundle\DBAL\Types\AbstractSetType */
     $class = $constraint->class;
     if (!class_exists($class)) {
         throw new TargetClassNotExistException('Target class not exist.');
     }
     $constraint->choices = $class::getValues();
     $constraint->multiple = true;
     parent::validate($value, $constraint);
 }
Esempio n. 6
0
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if (!$constraint instanceof Enum) {
         throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\Enum');
     }
     $constraint->choices = null;
     $constraint->callback = null;
     if (!$constraint->enum) {
         throw new ConstraintDefinitionException('"enum" must be specified on constraint Enum');
     }
     if (!$this->enumRegistry->has($constraint->enum)) {
         throw new ConstraintDefinitionException(sprintf('"enum" "%s" on constraint Enum does not exist', $constraint->enum));
     }
     $constraint->choices = array_keys($this->enumRegistry->get($constraint->enum)->getChoices());
     parent::validate($value, $constraint);
 }