class AllowedValuesConstraintValidator in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/AllowedValuesConstraintValidator.php \Drupal\Core\Validation\Plugin\Validation\Constraint\AllowedValuesConstraintValidator
Validates the AllowedValues constraint.
Hierarchy
- class \Symfony\Component\Validator\ConstraintValidator implements ConstraintValidatorInterface
- class \Symfony\Component\Validator\Constraints\ChoiceValidator
- class \Drupal\Core\Validation\Plugin\Validation\Constraint\AllowedValuesConstraintValidator implements ContainerInjectionInterface uses TypedDataAwareValidatorTrait
- class \Symfony\Component\Validator\Constraints\ChoiceValidator
Expanded class hierarchy of AllowedValuesConstraintValidator
File
- core/
lib/ Drupal/ Core/ Validation/ Plugin/ Validation/ Constraint/ AllowedValuesConstraintValidator.php, line 22 - Contains \Drupal\Core\Validation\Plugin\Validation\Constraint\AllowedValuesConstraintValidator.
Namespace
Drupal\Core\Validation\Plugin\Validation\ConstraintView source
class AllowedValuesConstraintValidator extends ChoiceValidator implements ContainerInjectionInterface {
use TypedDataAwareValidatorTrait;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('current_user'));
}
/**
* Constructs a new AllowedValuesConstraintValidator.
*
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
*/
public function __construct(AccountInterface $current_user) {
$this->currentUser = $current_user;
}
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint) {
$typed_data = $this
->getTypedData();
if ($typed_data instanceof OptionsProviderInterface) {
$allowed_values = $typed_data
->getSettableValues($this->currentUser);
$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);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AllowedValuesConstraintValidator:: |
protected | property | The current user. | |
AllowedValuesConstraintValidator:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
AllowedValuesConstraintValidator:: |
public | function |
Checks if the passed value is valid. Overrides ChoiceValidator:: |
|
AllowedValuesConstraintValidator:: |
public | function | Constructs a new AllowedValuesConstraintValidator. | |
ConstraintValidator:: |
protected | property | 3 | |
ConstraintValidator:: |
protected | function | Wrapper for {@link ExecutionContextInterface::buildViolation} that supports the 2.4 context API. | |
ConstraintValidator:: |
protected | function | Wrapper for {@link ExecutionContextInterface::buildViolation} that supports the 2.4 context API. | |
ConstraintValidator:: |
protected | function | Returns a string representation of the type of the value. | |
ConstraintValidator:: |
protected | function | Returns a string representation of the value. | |
ConstraintValidator:: |
protected | function | Returns a string representation of a list of values. | |
ConstraintValidator:: |
public | function |
Initializes the constraint validator. Overrides ConstraintValidatorInterface:: |
1 |
ConstraintValidator:: |
constant | Whether to cast objects with a "__toString()" method to strings. | ||
ConstraintValidator:: |
constant | Whether to format {@link \DateTime} objects as RFC-3339 dates ("Y-m-d H:i:s"). | ||
TypedDataAwareValidatorTrait:: |
public | function | Gets the typed data object for the validated value. |