function ConstraintManager::create

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Validation/ConstraintManager.php \Drupal\Core\Validation\ConstraintManager::create()
  2. 10 core/lib/Drupal/Core/Validation/ConstraintManager.php \Drupal\Core\Validation\ConstraintManager::create()
  3. 9 core/lib/Drupal/Core/Validation/ConstraintManager.php \Drupal\Core\Validation\ConstraintManager::create()
  4. 8.9.x core/lib/Drupal/Core/Validation/ConstraintManager.php \Drupal\Core\Validation\ConstraintManager::create()

Creates a validation constraint.

Parameters

string $name: The name or plugin id of the constraint.

array<string, mixed>|null $options: The options to pass to the constraint class. Required and supported options depend on the constraint class.

Return value

\Symfony\Component\Validator\Constraint A validation constraint plugin.

File

core/lib/Drupal/Core/Validation/ConstraintManager.php, line 82

Class

ConstraintManager
Constraint plugin manager.

Namespace

Drupal\Core\Validation

Code

public function create($name, $options) {
  if (!is_array($options)) {
    // Plugins need an array as configuration, so make sure we have one.
    // The constraint classes support passing the options as part of the
    // 'value' key also. Add a boolean flag property noting that the options
    // were not passed as an array for deprecation purposes in
    // ConstraintFactory::createInstance().
    // @phpstan-ignore isset.variable
    $options = isset($options) ? [
      'value' => $options,
      '_options_not_passed_as_array' => TRUE,
    ] : [];
  }
  if (!empty($options) && (isset($options['_options_not_passed_as_array']) || array_is_list($options))) {
    @trigger_error(sprintf('Passing any non-associative-array options to configure constraint plugin "%s" is deprecated in drupal:11.4.0 and will not be supported in drupal:12.0.0. See https://www.drupal.org/node/3554746', $name), E_USER_DEPRECATED);
  }
  return $this->createInstance($name, $options);
}

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