function NotNullConstraintValidator::validate

Same name in other branches
  1. 9 core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/NotNullConstraintValidator.php \Drupal\Core\Validation\Plugin\Validation\Constraint\NotNullConstraintValidator::validate()
  2. 8.9.x core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/NotNullConstraintValidator.php \Drupal\Core\Validation\Plugin\Validation\Constraint\NotNullConstraintValidator::validate()
  3. 11.x core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/NotNullConstraintValidator.php \Drupal\Core\Validation\Plugin\Validation\Constraint\NotNullConstraintValidator::validate()

phpcs:ignore Drupal.Commenting.FunctionComment.VoidReturn

Return value

void

File

core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/NotNullConstraintValidator.php, line 27

Class

NotNullConstraintValidator
NotNull constraint validator.

Namespace

Drupal\Core\Validation\Plugin\Validation\Constraint

Code

public function validate($value, Constraint $constraint) {
    $typed_data = $this->getTypedData();
    // TRICKY: the Mapping and Sequence data types both extend ArrayElement
    // (which implements ComplexDataInterface), but configuration schema sees a
    // substantial difference between an empty sequence/mapping and NULL. So we
    // want to make sure we don't treat an empty array as NULL.
    if (($typed_data instanceof ListInterface || $typed_data instanceof ComplexDataInterface) && !$typed_data instanceof ArrayElement && $typed_data->isEmpty()) {
        $value = NULL;
    }
    parent::validate($value, $constraint);
}

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