You are here

public function ComplexDataConstraint::__construct in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/ComplexDataConstraint.php \Drupal\Core\Validation\Plugin\Validation\Constraint\ComplexDataConstraint::__construct()

Initializes the constraint with options.

You should pass an associative array. The keys should be the names of existing properties in this class. The values should be the value for these properties.

Alternatively you can override the method getDefaultOption() to return the name of an existing property. If no associative array is passed, this property is set instead.

You can force that certain options are set by overriding getRequiredOptions() to return the names of these options. If any option is not set here, an exception is thrown.

Parameters

mixed $options The options (as associative array): or the value for the default option (any other type)

Throws

InvalidOptionsException When you pass the names of non-existing options

MissingOptionsException When you don't pass any of the options returned by getRequiredOptions()

ConstraintDefinitionException When you don't pass an associative array, but getDefaultOption() returns null

Overrides Constraint::__construct

File

core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/ComplexDataConstraint.php, line 34
Contains \Drupal\Core\Validation\Plugin\Validation\Constraint\ComplexDataConstraint.

Class

ComplexDataConstraint
Complex data constraint.

Namespace

Drupal\Core\Validation\Plugin\Validation\Constraint

Code

public function __construct($options = NULL) {

  // Allow skipping the 'properties' key in the options.
  if (is_array($options) && !array_key_exists('properties', $options)) {
    $options = array(
      'properties' => $options,
    );
  }
  parent::__construct($options);
  $constraint_manager = \Drupal::service('validation.constraint');

  // Instantiate constraint objects for array definitions.
  foreach ($this->properties as &$constraints) {
    foreach ($constraints as $id => $options) {
      if (!is_object($options)) {
        $constraints[$id] = $constraint_manager
          ->create($id, $options);
      }
    }
  }
}