public static function DoubleField::validateAllowedValues in Double Field 4.x
Same name and namespace in other branches
- 8.3 src/Plugin/Field/FieldType/DoubleField.php \Drupal\double_field\Plugin\Field\FieldType\DoubleField::validateAllowedValues()
Element validate callback for subfield allowed values.
Parameters
array $element: An associative array containing the properties and children of the generic form element.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form for the form this element belongs to.
See also
\Drupal\Core\Render\Element\FormElement::processPattern()
File
- src/
Plugin/ Field/ FieldType/ DoubleField.php, line 478
Class
- DoubleField
- Plugin implementation of the 'double_field' field type.
Namespace
Drupal\double_field\Plugin\Field\FieldTypeCode
public static function validateAllowedValues(array $element, FormStateInterface $form_state) : void {
$values = static::extractAllowedValues($element['#value']);
// Check if keys are valid for the field type.
foreach ($values as $key => $value) {
switch ($element['#storage_type']) {
case 'string':
// @see \Drupal\options\Plugin\Field\FieldType\ListStringItem::validateAllowedValue()
if (mb_strlen($key) > $element['#storage_maxlength']) {
$error_message = t('Allowed values list: each key must be a string at most @maxlength characters long.', [
'@maxlength' => $element['#storage_maxlength'],
]);
$form_state
->setError($element, $error_message);
}
break;
case 'integer':
// @see \Drupal\options\Plugin\Field\FieldType\ListIntegerItem::validateAllowedValue()
if (!preg_match('/^-?\\d+$/', $key)) {
$form_state
->setError($element, 'Allowed values list: keys must be integers.');
}
break;
case 'float':
case 'numeric':
// @see \Drupal\options\Plugin\Field\FieldType\ListFloatItem::validateAllowedValue()
if (!is_numeric($key)) {
$form_state
->setError($element, 'Allowed values list: each key must be a valid integer or decimal.');
}
break;
}
}
$form_state
->setValueForElement($element, $values);
}