I’m very new to drupal and it has been one hell of a ride already.
I’m creating a custom module where people can fill out a form to apply for a job. Of course I want to implement form validation but it goes wrong when I want to validate the uploaded file.
I can’t seem to find a clear way to validate uploaded files, like anywhere. I’m trying to rely on my php knowledge but of course it is not entirly te same when it comes down to drupal.
I’m providing you guys with something I had now, which clearly doesn’t work, but I would like to ask what you would say is the best way to validate uploaded files? And without installing third party modules..
What I tried:
// Validate the uploaded file
$validators = ['file_validate_extensions' => ['docx doc pdf jpg jpeg png']];
$file = file_save_upload('cv', $validators, FALSE, 0);
if (!$file) {
// Add error message if the file isn't validated
$form_state->setErrorByName('cv', $this->t('Uw CV heeft een niet-toegestane extensie. Probeer het opnieuw.'));
} else {
// Set the correct file in the form state
$form_state->setValue('cv', $file);
}
Unfortunately, file_save_upload is not recognized because it’s deprecated and drupals documentation is a bit confusing to me..
Thanks in advance!