I have followed the official Form API Documentation, but this code don't work for me on Drupal 8:
public function buildForm(array $form, FormStateInterface $form_state) {
$form['upload'] = array(
'#title' => t('Upload Images'),
'#type' => 'managed_file',
'#upload_location' => 'public://image_example_images/',
'#element_validate' => array('file_generic_settings_max_filesize'),
);
return $form;
}
private function file_generic_settings_max_filesize($element, &$form_state) {
form_error($element, 'This is an error.');
}
I have trying to remplace form_error() by $form_state->setErrorByName() but same problem, the file is uploaded and the validation is not called.
Have you a solution please ? The documentation of #element_validate is outdated ?
My objective:
Upload a lot of images, use a custom validation and insert all images thumbnails inside a <div class="preview"></div>
with a field for name each images.
Example: (After upload, display thumbnails and title field for name all each images)
Else, after upload, for display Thumbnails and other field (title and description) for each image, I need to use #value_callback, #process, #attached (manage field/display with js after upload) or an other option please ?
Thanks.