/** * {@inheritdoc} */ public function configureOptions(OptionsResolver $resolver) { $resolver->setRequired('enum')->setDefault('choices_as_values', true)->setAllowedValues('enum', function ($name) { return $this->enumRegistry->has($name); })->setDefault('choices', function (Options $options) { return array_flip($this->enumRegistry->get($options['enum'])->getChoices()); }); }
/** * {@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); }