diff --git a/code/model/UserDefinedForm.php b/code/model/UserDefinedForm.php index fdac00e2c..ee3c6729c 100755 --- a/code/model/UserDefinedForm.php +++ b/code/model/UserDefinedForm.php @@ -51,8 +51,7 @@ class UserDefinedForm extends Page { 'HideFieldLabels' => 'Boolean', 'DisplayErrorMessagesAtTop' => 'Boolean', 'DisableAuthenicatedFinishAction' => 'Boolean', - 'DisableCsrfSecurityToken' => 'Boolean', - 'CustomIndividualFileSize' => 'Int' + 'DisableCsrfSecurityToken' => 'Boolean' ); /** @@ -209,7 +208,6 @@ public function getCMSFields() { $export->setExportColumns($columns); $submissions->setConfig($config); - $fields->addFieldToTab('Root.FormOptions', new NumericField('CustomIndividualFileSize', _t('UserDefinedForm.CustomIndividualFileSize', 'Enter in the Custom file size for indivual upload fields.'))); $fields->addFieldToTab('Root.Submissions', $submissions); $fields->addFieldToTab('Root.FormOptions', new CheckboxField('DisableSaveSubmissions', _t('UserDefinedForm.SAVESUBMISSIONS', 'Disable Saving Submissions to Server'))); diff --git a/code/model/editableformfields/EditableFileField.php b/code/model/editableformfields/EditableFileField.php index 68ab1dec9..0538b9264 100755 --- a/code/model/editableformfields/EditableFileField.php +++ b/code/model/editableformfields/EditableFileField.php @@ -12,6 +12,10 @@ class EditableFileField extends EditableFormField { private static $plural_names = 'File Fields'; + private static $db = array( + 'MaxFileSize' => 'Int' + ); + private static $has_one = array( 'Folder' => 'Folder' // From CustomFields ); @@ -45,6 +49,22 @@ public function getCMSFields() { "Files uploaded through this field could be publicly accessible if the exact URL is known") . "

"), "Type"); + $fields->addFieldToTab('Root.Main', + $numericField = new NumericField('MaxFileSize', + _t('EditableUploadField.MaxFileSize', + 'Enter in the maximum file size in MB for this upload field.')) + ); + $maxUpload = File::ini2bytes(ini_get('upload_max_filesize')); + $maxPost= File::ini2bytes(ini_get('post_max_size')); + $maxSizeBytes = min($maxUpload, $maxPost); + $numericField->setDescription( + sprintf( + _t('EditableUploadField.MaximumFileSize', + 'Note: Maximum php allowed size is %s MB'), + (round($maxSizeBytes / 1024.0, 1) /1024) + ) + ); + return $fields; } @@ -73,6 +93,31 @@ public function getFormField() { return $field; } + /** + * Validate if the file is over a certain size + * + * @return boolean + */ + public function validateField($data, $form) { + if ($this->MaxFileSize) { + foreach ($data as $file) { + if (isset($file['size'])) { + if (($this->MaxFileSize * 1024 * 1024) < $file['size']) { + $form->addErrorMessage($this->Name, + sprintf( + _t('EditableUploadField.FileSizeExceded', + 'File size can not be over %s mb.'), + $this->MaxFileSize), + 'error', + false + ); + } + } + } + } + return true; + } + /** * Return the value for the database, link to the file is stored as a