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
rather than:
Filesize is too large…
I can see that these messages are set in Upload_Validator
, and for FileField
it says:
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!