mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENHANCEMENT Disable UploadField when handling unsaved relations (fixes #7187)
This commit is contained in:
parent
16527f7fef
commit
eff93bdd5b
@ -305,6 +305,12 @@ class UploadField extends FileField {
|
||||
);
|
||||
}
|
||||
|
||||
public function extraClass() {
|
||||
if($this->isDisabled()) $this->addExtraClass('disabled');
|
||||
if($this->isReadonly()) $this->addExtraClass('readonly');
|
||||
return parent::extraClass();
|
||||
}
|
||||
|
||||
public function Field($properties = array()) {
|
||||
$record = $this->getRecord();
|
||||
$name = $this->getName();
|
||||
@ -597,6 +603,21 @@ class UploadField extends FileField {
|
||||
|
||||
if (isset($name) && isset($record)) return $record->getRelationClass($name);
|
||||
}
|
||||
|
||||
public function isDisabled() {
|
||||
return (parent::isDisabled() || !$this->isSaveable());
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the field can be saved into a database record.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isSaveable() {
|
||||
$record = $this->getRecord();
|
||||
// Don't allow upload or edit of a relation when the underlying record hasn't been persisted yet
|
||||
return (!$record || !$this->managesRelation() || $record->exists());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -26,6 +26,12 @@
|
||||
<% end_if %>
|
||||
</ul>
|
||||
<% if isDisabled || isReadonly %>
|
||||
<% if isSaveable %>
|
||||
<% else %>
|
||||
<div class="ss-uploadfield-item">
|
||||
<em><% _t('FileIFrameField.ATTACHONCESAVED2', 'Files can be attached once you have saved the record for the first time.') %></em>
|
||||
</div>
|
||||
<% end_if %>
|
||||
<% else %>
|
||||
<div class="ss-uploadfield-item ss-uploadfield-addfile<% if $Items && $displayInput %> borderTop<% end_if %>" <% if not $displayInput %>style="display: none;"<% end_if %>>
|
||||
<div class="ss-uploadfield-item-preview ss-uploadfield-dropzone ui-corner-all">
|
||||
|
@ -465,6 +465,28 @@
|
||||
|
||||
}
|
||||
|
||||
function testIsSaveable() {
|
||||
$form = $this->getMockForm();
|
||||
|
||||
$field = new UploadField('MyField');
|
||||
$this->assertTrue($field->isSaveable(), 'Field without relation is always marked as saveable');
|
||||
|
||||
$field = new UploadField('HasOneFile');
|
||||
$this->assertTrue($field->isSaveable(), 'Field with has_one relation is saveable without record on form');
|
||||
|
||||
$field = new UploadField('HasOneFile');
|
||||
$newRecord = new UploadFieldTest_Record();
|
||||
$form->loadDataFrom($newRecord);
|
||||
$field->setForm($form);
|
||||
$this->assertFalse($field->isSaveable(), 'Field with has_one relation not saveable with new record on form');
|
||||
|
||||
$field = new UploadField('HasOneFile');
|
||||
$existingRecord = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
||||
$form->loadDataFrom($existingRecord);
|
||||
$field->setForm($form);
|
||||
$this->assertTrue($field->isSaveable(), 'Field with has_one relation saveable with saved record on form');
|
||||
}
|
||||
|
||||
function testSelect() {
|
||||
$this->loginWithPermission('ADMIN');
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user