function NumericItemBase::getConstraints

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

Overrides TypedData::getConstraints

1 call to NumericItemBase::getConstraints()
IntegerItem::getConstraints in core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php
Gets a list of validation constraints.
1 method overrides NumericItemBase::getConstraints()
IntegerItem::getConstraints in core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php
Gets a list of validation constraints.

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldType/NumericItemBase.php, line 75

Class

NumericItemBase
Base class for numeric configurable field types.

Namespace

Drupal\Core\Field\Plugin\Field\FieldType

Code

public function getConstraints() {
  $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
  $constraints = parent::getConstraints();
  $settings = $this->getSettings();
  $label = $this->getFieldDefinition()
    ->getLabel();
  if (isset($settings['min']) && $settings['min'] !== '') {
    $min = $settings['min'];
    $constraints[] = $constraint_manager->create('ComplexData', [
      'properties' => [
        'value' => [
          'Range' => [
            'min' => $min,
            'minMessage' => $this->t('%name: the value may be no less than %min.', [
              '%name' => $label,
              '%min' => $min,
            ]),
          ],
        ],
      ],
    ]);
  }
  if (isset($settings['max']) && $settings['max'] !== '') {
    $max = $settings['max'];
    $constraints[] = $constraint_manager->create('ComplexData', [
      'properties' => [
        'value' => [
          'Range' => [
            'max' => $max,
            'maxMessage' => $this->t('%name: the value may be no greater than %max.', [
              '%name' => $label,
              '%max' => $max,
            ]),
          ],
        ],
      ],
    ]);
  }
  return $constraints;
}

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