class NotNullConstraintValidator

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

NotNull constraint validator.

Overrides the symfony validator to handle empty Typed Data structures.

Hierarchy

Expanded class hierarchy of NotNullConstraintValidator

File

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

Namespace

Drupal\Core\Validation\Plugin\Validation\Constraint
View source
class NotNullConstraintValidator extends NotNullValidator {
    use TypedDataAwareValidatorTrait;
    
    /**
     * {@inheritdoc}
     *
     * phpcs:ignore Drupal.Commenting.FunctionComment.VoidReturn
     * @return void
     */
    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);
    }

}

Members

Title Sort descending Modifiers Object type Summary
NotNullConstraintValidator::validate public function phpcs:ignore Drupal.Commenting.FunctionComment.VoidReturn
TypedDataAwareValidatorTrait::getTypedData public function Gets the typed data object for the validated value.

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