Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the file required and prevent submitForm to be called in case file_save_upload does not validate extensions #20

Open
Nicolas-Bouteille opened this issue Oct 29, 2020 · 0 comments

Comments

@Nicolas-Bouteille
Copy link

Nicolas-Bouteille commented Oct 29, 2020

It seems attribute "#required" on a 'file' FormElement leads to a bug that displays a missing file error no matter what...
However, we have our convenient custom validator for that '#element_validate' => ['::validateFileupload'],
The first thing file_save_upload does, it checks there is actually a file. If none, it returns NULL. In case of error though, it returns FALSE.
So we can make a first check and add a specific error message for missing file:

$file = file_save_upload('csvimport', $validators, FALSE, 0, FileSystemInterface::EXISTS_REPLACE);
if ($file === NULL) {
 $form_state->setErrorByName('csvimport', t("Missing file"));
}
elseif ($file) { // != FALSE
}
else {
  // The line below prevents the submitForm function to be called. The errors added by file_save_upload will be displayed
  $form_state->setErrorByName('csvimport', '');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant