如何在Silverstripe用户表单模块中的FileFields中验证上载的文件大小

We have a Silverstripe website where the admins can create online forms using the userform module.

To allow a file to be uploaded via the form the admin add a "File Upload Field" to the form. This adds a Filefield to the form.

The Filefield is a simplified version of UploadField which allows a number of validation options, including maximum upload file size.

Do these options exist for Filefield?

My issue is I need to keep the global PHP upload_max_filesize and post_max_size high for backend CMS functionality but I want to limit the front end Filefield uploads to 8meg.

Is it possible to validate Filefield in the userform module so it fails/ displays a message if the file is over a certain size that is small than the PHP variables?

Doesn't look like there's built in functionality for it. The documentation states the file size is limited by the webserver [1].

However, FileField uses the Upload class' validate() function which in its turn uses the Upload_Validator class' validate() function [2] which checks for a max file size in the Config.

This means you can add the following to your config file to restrict file size:

# Don't forget to flush afterwards :-)
Upload_Validator:
  default_max_file_size: '1m' # 1 megabyte

Please take note that this is a default setting, which means it probably affects all other UploadFields as well. You can however set a limit on the other UploadFields that need to handle larger files.

[1] https://github.com/silverstripe/silverstripe-userforms/blob/master/docs/en/user-documentation.md#file-upload-field

[2] https://github.com/silverstripe/silverstripe-framework/blob/master/filesystem/Upload.php#L536