ENHANCEMENT Disable UploadField when handling unsaved relations (fixes #7187)

This commit is contained in:
Ingo Schommer 2012-06-15 17:44:34 +02:00
parent 16527f7fef
commit eff93bdd5b
3 changed files with 49 additions and 0 deletions

View File

@ -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());
}
}
/**

View File

@ -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">

View File

@ -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');