Add attachOnUpload option to FileUpload formwidget

Fixes #3501
This commit is contained in:
Luke Towers 2018-06-04 16:22:23 -06:00
parent 5ec5336a79
commit 0086a47b80
1 changed files with 10 additions and 3 deletions

View File

@ -71,6 +71,11 @@ class FileUpload extends FormWidgetBase
*/
public $useCaption = true;
/**
* @var boolean Automatically attaches the uploaded file on upload if the parent record exists instead of using deferred binding to attach on save of the parent record. Defaults to false.
*/
public $attachOnUpload = false;
//
// Object properties
//
@ -92,7 +97,8 @@ class FileUpload extends FormWidgetBase
'fileTypes',
'mimeTypes',
'thumbOptions',
'useCaption'
'useCaption',
'attachOnUpload',
]);
if ($this->formField->disabled) {
@ -395,10 +401,11 @@ class FileUpload extends FormWidgetBase
$file->save();
/**
* Attach directly to the parent model if it exists,
* Attach directly to the parent model if it exists and attachOnUpload has been set to true
* else attach via deferred binding
*/
if (@$fileRelation->getParent()->exists) {
$parent = $fileRelation->getParent();
if ($this->attachOnUpload && $parent && $parent->exists) {
$fileRelation->add($file);
} else {
$fileRelation->add($file, $this->sessionKey);