You are here

public function StringItem::getConstraints in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StringItem.php \Drupal\Core\Field\Plugin\Field\FieldType\StringItem::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/Plugin/Field/FieldType/StringItem.php, line 57
Contains \Drupal\Core\Field\Plugin\Field\FieldType\StringItem.

Class

StringItem
Defines the 'string' entity field type.

Namespace

Drupal\Core\Field\Plugin\Field\FieldType

Code

public function getConstraints() {
  $constraints = parent::getConstraints();
  if ($max_length = $this
    ->getSetting('max_length')) {
    $constraint_manager = \Drupal::typedDataManager()
      ->getValidationConstraintManager();
    $constraints[] = $constraint_manager
      ->create('ComplexData', array(
      'value' => array(
        'Length' => array(
          'max' => $max_length,
          'maxMessage' => t('%name: may not be longer than @max characters.', array(
            '%name' => $this
              ->getFieldDefinition()
              ->getLabel(),
            '@max' => $max_length,
          )),
        ),
      ),
    ));
  }
  return $constraints;
}