You are here

public function NumericItemBase::getConstraints in Drupal 9

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

Gets a list of validation constraints.

Return value

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

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;
}