function FieldItemList::getConstraints

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

Overrides TypedData::getConstraints

1 method overrides FieldItemList::getConstraints()
EntityReferenceFieldItemList::getConstraints in core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php
Gets a list of validation constraints.

File

core/lib/Drupal/Core/Field/FieldItemList.php, line 261

Class

FieldItemList
Represents an entity field; that is, a list of field item objects.

Namespace

Drupal\Core\Field

Code

public function getConstraints() {
    $constraints = parent::getConstraints();
    // Check that the number of values doesn't exceed the field cardinality. For
    // form submitted values, this can only happen with 'multiple value'
    // widgets.
    $cardinality = $this->getFieldDefinition()
        ->getFieldStorageDefinition()
        ->getCardinality();
    if ($cardinality != FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
        $constraints[] = $this->getTypedDataManager()
            ->getValidationConstraintManager()
            ->create('Count', [
            'max' => $cardinality,
            'maxMessage' => t('%name: this field cannot hold more than @count values.', [
                '%name' => $this->getFieldDefinition()
                    ->getLabel(),
                '@count' => $cardinality,
            ]),
        ]);
    }
    return $constraints;
}

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