FieldItemList::getConstraints

public FieldItemList::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

File

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

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', array(
        'max' => $cardinality,
        'maxMessage' => t('%name: this field cannot hold more than @count values.', array('%name' => $this->getFieldDefinition()->getLabel(), '@count' => $cardinality)),
      ));
  }

  return $constraints;
}
doc_Drupal
2016-10-29 09:11:54
Comments
Leave a Comment

Please login to continue.