function IntegerItem::getConstraints

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php \Drupal\Core\Field\Plugin\Field\FieldType\IntegerItem::getConstraints()
  2. 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php \Drupal\Core\Field\Plugin\Field\FieldType\IntegerItem::getConstraints()
  3. 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php \Drupal\Core\Field\Plugin\Field\FieldType\IntegerItem::getConstraints()

Overrides NumericItemBase::getConstraints

File

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

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' => $this->t('%name: The integer must be larger or equal to %min.', [
            '%name' => $this->getFieldDefinition()
              ->getLabel(),
            '%min' => 0,
          ]),
        ],
      ],
    ]);
  }
  return $constraints;
}

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