function MediaLibraryWidget::validateRequired
Same name in other branches
- 9 core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php \Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget::validateRequired()
- 11.x core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php \Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget::validateRequired()
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 999
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;
}
// If user has no access, the validation isn't needed.
if (isset($element['#access']) && !$element['#access']) {
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, new TranslatableMarkup('@name field is required.', [
'@name' => $element['#title'],
]));
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.