NotNullConstraintValidator.php

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

Namespace

Drupal\Core\Validation\Plugin\Validation\Constraint

File

core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/NotNullConstraintValidator.php

View source
<?php

namespace Drupal\Core\Validation\Plugin\Validation\Constraint;

use Drupal\Core\TypedData\ComplexDataInterface;
use Drupal\Core\TypedData\ListInterface;
use Drupal\Core\TypedData\Validation\TypedDataAwareValidatorTrait;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\NotNullValidator;

/**
 * NotNull constraint validator.
 *
 * Overrides the symfony validator to handle empty Typed Data structures.
 */
class NotNullConstraintValidator extends NotNullValidator {
    use TypedDataAwareValidatorTrait;
    
    /**
     * {@inheritdoc}
     */
    public function validate($value, Constraint $constraint) {
        $typed_data = $this->getTypedData();
        if (($typed_data instanceof ListInterface || $typed_data instanceof ComplexDataInterface) && $typed_data->isEmpty()) {
            $value = NULL;
        }
        parent::validate($value, $constraint);
    }

}

Classes

Title Deprecated Summary
NotNullConstraintValidator NotNull constraint validator.

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