Merge pull request #374 from tractorcow/pulls/fix-3.2-compat

BUG Prevent subsequent write being triggered in onAfterWrite()
This commit is contained in:
Ingo Schommer 2015-10-29 11:40:04 +13:00
commit 7aba5e340c
3 changed files with 45 additions and 12 deletions

View File

@ -276,7 +276,12 @@ class EditableFormField extends DataObject {
public function onBeforeWrite() { public function onBeforeWrite() {
parent::onBeforeWrite(); parent::onBeforeWrite();
if($this->Name === 'Field') { // Set a field name.
if(!$this->Name) {
// New random name
$this->Name = $this->generateName();
} elseif($this->Name === 'Field') {
throw new ValidationException('Field name cannot be "Field"'); throw new ValidationException('Field name cannot be "Field"');
} }
@ -289,16 +294,21 @@ class EditableFormField extends DataObject {
} }
/** /**
* @return void * Generate a new non-conflicting Name value
*
* @return string
*/ */
public function onAfterWrite() { protected function generateName() {
parent::onAfterWrite(); do {
// Generate a new random name after this class
$class = get_class($this);
$entropy = substr(sha1(uniqid()), 0, 5);
$name = "{$class}_{$entropy}";
// Set a field name. // Check if it conflicts
if(!$this->Name) { $exists = EditableFormField::get()->filter('Name', $name)->count() > 0;
$this->Name = get_class($this) . '_' . $this->ID; } while($exists);
$this->write(); return $name;
}
} }
/** /**

View File

@ -82,9 +82,9 @@
this.on('addnewinline', function () { this.on('addnewinline', function () {
self.one('reload', function () { self.one('reload', function () {
//If fieldgroup, focus on the start marker //If fieldgroup, focus on the start marker
var $newField = self.find('.ss-gridfield-item').last() var $newField = self.find('.ss-gridfield-item').last(), $groupEnd;
if ($newField.attr('data-class') === 'EditableFieldGroupEnd') { if ($newField.attr('data-class') === 'EditableFieldGroupEnd') {
var $groupEnd = $newField; $groupEnd = $newField;
$groupEnd.prev().find('.col-Title input').focus(); $groupEnd.prev().find('.col-Title input').focus();
$newField = $groupEnd.add($groupEnd.prev()); $newField = $groupEnd.add($groupEnd.prev());
$groupEnd.css('visibility', 'hidden'); $groupEnd.css('visibility', 'hidden');
@ -101,7 +101,9 @@
setTimeout(function () { setTimeout(function () {
$newField.removeClass('newField').addClass('flashBackground'); $newField.removeClass('newField').addClass('flashBackground');
$(".cms-content-fields").scrollTop($(".cms-content-fields")[0].scrollHeight); $(".cms-content-fields").scrollTop($(".cms-content-fields")[0].scrollHeight);
$groupEnd.css('visibility', 'visible'); if($groupEnd) {
$groupEnd.css('visibility', 'visible');
}
}, 500); }, 500);
}); });
}); });

View File

@ -110,4 +110,25 @@ class EditableFormFieldTest extends FunctionalTest {
$this->assertNotContains('jpg', $formField->getValidator()->getAllowedExtensions()); $this->assertNotContains('jpg', $formField->getValidator()->getAllowedExtensions());
} }
/**
* Verify that unique names are automatically generated for each formfield
*/
public function testUniqueName() {
$textfield1 = new EditableTextField();
$this->assertEmpty($textfield1->Name);
// Write values
$textfield1->write();
$textfield2 = new EditableTextField();
$textfield2->write();
$checkboxField = new EditableCheckbox();
$checkboxField->write();
// Test values are in the expected format
$this->assertRegExp('/^EditableTextField_.+/', $textfield1->Name);
$this->assertRegExp('/^EditableTextField_.+/', $textfield2->Name);
$this->assertRegExp('/^EditableCheckbox_.+/', $checkboxField->Name);
$this->assertNotEquals($textfield1->Name, $textfield2->Name);
}
} }