function NumericItemBase::getConstraints

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

Overrides TypedData::getConstraints

2 calls to NumericItemBase::getConstraints()
DecimalItem::getConstraints in core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DecimalItem.php
Gets a list of validation constraints.
IntegerItem::getConstraints in core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php
Gets a list of validation constraints.
2 methods override NumericItemBase::getConstraints()
DecimalItem::getConstraints in core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DecimalItem.php
Gets a list of validation constraints.
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', [
            'value' => [
                'Range' => [
                    'min' => $min,
                    'minMessage' => 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', [
            'value' => [
                'Range' => [
                    'max' => $max,
                    'maxMessage' => 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.