You are here

public function IntegerItem::getConstraints in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php \Drupal\Core\Field\Plugin\Field\FieldType\IntegerItem::getConstraints()

Gets a list of validation constraints.

Return value

array Array of constraints, each being an instance of \Symfony\Component\Validator\Constraint.

Overrides NumericItemBase::getConstraints

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php, line 61

Class

IntegerItem
Defines the 'integer' field type.

Namespace

Drupal\Core\Field\Plugin\Field\FieldType

Code

public function getConstraints() {
  $constraints = parent::getConstraints();

  // If this is an unsigned integer, add a validation constraint for the
  // integer to be positive.
  if ($this
    ->getSetting('unsigned')) {
    $constraint_manager = \Drupal::typedDataManager()
      ->getValidationConstraintManager();
    $constraints[] = $constraint_manager
      ->create('ComplexData', [
      'value' => [
        'Range' => [
          'min' => 0,
          'minMessage' => t('%name: The integer must be larger or equal to %min.', [
            '%name' => $this
              ->getFieldDefinition()
              ->getLabel(),
            '%min' => 0,
          ]),
        ],
      ],
    ]);
  }
  return $constraints;
}