diff --git a/forms/UploadField.php b/forms/UploadField.php index f7c6e1056..cb0683328 100644 --- a/forms/UploadField.php +++ b/forms/UploadField.php @@ -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()); + } } /** diff --git a/templates/UploadField.ss b/templates/UploadField.ss index 340d4c602..6a10370cd 100644 --- a/templates/UploadField.ss +++ b/templates/UploadField.ss @@ -26,6 +26,12 @@ <% end_if %> <% if isDisabled || isReadonly %> + <% if isSaveable %> + <% else %> +
+ <% _t('FileIFrameField.ATTACHONCESAVED2', 'Files can be attached once you have saved the record for the first time.') %> +
+ <% end_if %> <% else %>
style="display: none;"<% end_if %>>
diff --git a/tests/forms/uploadfield/UploadFieldTest.php b/tests/forms/uploadfield/UploadFieldTest.php index 7340cbfd4..3e7d1051e 100644 --- a/tests/forms/uploadfield/UploadFieldTest.php +++ b/tests/forms/uploadfield/UploadFieldTest.php @@ -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');