public static function Select2Widget::validateElement in Select 2 8
Form validation handler for widget elements.
Parameters
array $element: The form element.
\Drupal\Core\Form\FormStateInterface $form_state: The form state.
Overrides OptionsWidgetBase::validateElement
File
- src/
Plugin/ Field/ FieldWidget/ Select2Widget.php, line 81
Class
- Select2Widget
- Plugin implementation of the 'select2' widget.
Namespace
Drupal\select2\Plugin\Field\FieldWidgetCode
public static function validateElement(array $element, FormStateInterface $form_state) {
if ($element['#required'] && $element['#value'] == '') {
$form_state
->setError($element, t('@name field is required.', [
'@name' => $element['#title'],
]));
}
// Massage submitted form values.
// Drupal\Core\Field\WidgetBase::submit() expects values as
// an array of values keyed by delta first, then by column, while our
// widgets return the opposite.
if (is_array($element['#value'])) {
$values = array_values($element['#value']);
}
else {
$values = [
$element['#value'],
];
}
// Filter out the '' option. Use a strict comparison, because
// 0 == 'any string'.
$index = array_search('', $values, TRUE);
if ($index !== FALSE) {
unset($values[$index]);
}
$items = static::prepareFieldValues($values, $element);
$form_state
->setValueForElement($element, $items);
}