Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions code/model/UserDefinedForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ class UserDefinedForm extends Page {
'HideFieldLabels' => 'Boolean',
'DisplayErrorMessagesAtTop' => 'Boolean',
'DisableAuthenicatedFinishAction' => 'Boolean',
'DisableCsrfSecurityToken' => 'Boolean',
'CustomIndividualFileSize' => 'Int'
'DisableCsrfSecurityToken' => 'Boolean'
);

/**
Expand Down Expand Up @@ -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')));

Expand Down
45 changes: 45 additions & 0 deletions code/model/editableformfields/EditableFileField.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class EditableFileField extends EditableFormField {

private static $plural_names = 'File Fields';

private static $db = array(
'MaxFileSize' => 'Int'

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. ;)

);

private static $has_one = array(
'Folder' => 'Folder' // From CustomFields
);
Expand Down Expand Up @@ -45,6 +49,22 @@ public function getCMSFields() {
"Files uploaded through this field could be publicly accessible if the exact URL is known")
. "</p>"), "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',

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In templates you can use variables. Eg.

_t(
    'EditableUploadField.MaximumFileSize',
    'Note: Maximum php allowed size is {max} MB',
   array('max' => round($maxSizeBytes / 1024.0, 1) / 1024)
);

'Note: Maximum php allowed size is %s MB'),
(round($maxSizeBytes / 1024.0, 1) /1024)
)
);

return $fields;
}

Expand Down Expand Up @@ -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,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can set the filesize in EditableFileField::getField and save having to duplicate all of this validation.

$field->getValidator()->setAllowedMaxFileSize(array('*' => $max));

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
Expand Down