class ClassResolverConstraint

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/ClassResolverConstraint.php \Drupal\Core\Validation\Plugin\Validation\Constraint\ClassResolverConstraint

Checks if a method on a service or instantiated object returns true.

For example to call the method 'isValidScheme' on the service 'stream_wrapper_manager', use: ['stream_wrapper_manager', 'isValidScheme']. It is also possible to use a class if it implements ContainerInjectionInterface. It will use the ClassResolver to resolve the class and return an instance. Then it will call the configured method on that instance.

The called method should return TRUE when the result is valid. All other values will be considered as invalid.

Attributes

#[Constraint(id: 'ClassResolver', label: new TranslatableMarkup('Call a method on a service', [], [ 'context' => 'Validation', ]), type: FALSE)]

Hierarchy

  • class \Drupal\Core\Validation\Plugin\Validation\Constraint\ClassResolverConstraint extends \Symfony\Component\Validator\Constraint

Expanded class hierarchy of ClassResolverConstraint

File

core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/ClassResolverConstraint.php, line 25

Namespace

Drupal\Core\Validation\Plugin\Validation\Constraint
View source
class ClassResolverConstraint extends SymfonyConstraint {
  
  /**
   * Class or service.
   *
   * @var array
   */
  public string $classOrService;
  
  /**
   * Method to call.
   *
   * @var string
   */
  public string $method;
  public function __construct(mixed $options = NULL, ?string $classOrService = NULL, ?string $method = NULL, public string $message = "Calling '@method' method with value '@value' on '@classOrService' evaluated as invalid.", ?array $groups = NULL, mixed $payload = NULL) {
    parent::__construct($options, $groups, $payload);
    $this->classOrService = $classOrService ?? $this->classOrService;
    $this->method = $method ?? $this->method;
  }
  
  /**
   * {@inheritdoc}
   */
  public function getRequiredOptions() : array {
    return [
      'classOrService',
      'method',
    ];
  }

}

Members

Title Sort descending Modifiers Object type Summary
ClassResolverConstraint::$classOrService public property Class or service.
ClassResolverConstraint::$method public property Method to call.
ClassResolverConstraint::getRequiredOptions public function
ClassResolverConstraint::__construct public function #[HasNamedArguments]

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.