I have a simple frontend form on Silverstripe 4. One of the fields is a
FileField for uploading a single file.
I’ve successfully added validation to the
FileField to set the allowed file size and extensions:
$fileField = FileField::create('Photo'); $fileField->getValidator()->setAllowedMaxFileSize("10M"); $fileField->getValidator()->setAllowedExtensions(['jpg','png']);
This works, but the validation messages are not making it through to the form, so if a user submits an image that it too large, it just seems to discard the file and says:
“Photo” is required
Filesize is too large…
Please set a validator on the form-object to get feedback about imposed filesize/extension restrictions.
However, even after reading the validation docs, I’m not sure how to do this. Is someone able to clarify exactly what is required here? Thanks!