public static function MediaLibraryWidget::validateRequired in Drupal 9
Validates whether the widget is required and contains values.
Parameters
array $element: The form element.
\Drupal\Core\Form\FormStateInterface $form_state: The form state.
array $form: The form array.
File
- core/
modules/ media_library/ src/ Plugin/ Field/ FieldWidget/ MediaLibraryWidget.php, line 957
Class
- MediaLibraryWidget
- Plugin implementation of the 'media_library_widget' widget.
Namespace
Drupal\media_library\Plugin\Field\FieldWidgetCode
public static function validateRequired(array $element, FormStateInterface $form_state, array $form) {
// If a remove button triggered submit, this validation isn't needed.
if (in_array([
static::class,
'removeItem',
], $form_state
->getSubmitHandlers(), TRUE)) {
return;
}
$field_state = static::getFieldState($element, $form_state);
// Trigger error if the field is required and no media is present. Although
// the Form API's default validation would also catch this, the validation
// error message is too vague, so a more precise one is provided here.
if (count($field_state['items']) === 0) {
$form_state
->setError($element, t('@name field is required.', [
'@name' => $element['#title'],
]));
}
}