Skip to content

Commit

Permalink
Enable better button functionality (silverstripe#897)
Browse files Browse the repository at this point in the history
Enable better button functionality

Co-authored-by: Bram de Leeuw <[email protected]>
Co-authored-by: Bram de Leeuw <[email protected]>
  • Loading branch information
3 people authored Jul 16, 2019
2 parents 1386f1b + 8703ace commit 75bec8c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
5 changes: 4 additions & 1 deletion code/Extension/UserFormFieldEditorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SilverStripe\UserForms\Extension;

use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\GridField\GridFieldPaginator;
use SilverStripe\Forms\Tab;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldButtonRow;
Expand Down Expand Up @@ -103,7 +104,9 @@ public function getFieldEditorGrid()
new GridFieldDeleteAction(),
new GridFieldToolbarHeader(),
new GridFieldOrderableRows('Sort'),
new GridFieldDetailForm(null, false, false)
new GridFieldDetailForm(),
// Betterbuttons prev and next is enabled by adding a GridFieldPaginator component
new GridFieldPaginator(999)
);

$editButton->removeExtraClass('grid-field__icon-action--hidden-on-hover');
Expand Down
10 changes: 10 additions & 0 deletions code/Model/EditableFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,16 @@ public function getCMSFields()
{
$fields = FieldList::create(TabSet::create('Root'));

// If created with (+) button
if ($this->ClassName === EditableFormField::class) {
$fieldClasses = $this->getEditableFieldClasses();
$fields->addFieldsToTab('Root.Main', [
DropdownField::create('ClassName', _t(__CLASS__.'.TYPE', 'Type'), $fieldClasses)
->setEmptyString(_t(__CLASS__ . 'TYPE_EMPTY', 'Select field type'))
]);
return $fields;
}

// Main tab
$fields->addFieldsToTab(
'Root.Main',
Expand Down
12 changes: 12 additions & 0 deletions code/Model/EditableFormField/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ public function php($data)
return false;
}

// When the record is unsaved and the classname is not set throw an error
if ((!$this->record || !$this->record->exists()) && (!isset($data['ClassName']) || empty($data['ClassName']))) {
$this->validationError(
'ClassName',
_t(
__CLASS__ . 'CLASSNAME_ERROR',
'You need to select a field type before you can create the field'
)
);
return false;
}

// Skip unsaved records
if (!$this->record || !$this->record->exists()) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion code/UserForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public function getCMSFields()
$config->addComponent(new GridFieldDeleteAction());
$config->addComponent(new GridFieldPageCount('toolbar-header-right'));
$config->addComponent($pagination = new GridFieldPaginator(25));
$config->addComponent(new GridFieldDetailForm(null, null, false));
$config->addComponent(new GridFieldDetailForm(null, true, false));
$config->addComponent(new GridFieldButtonRow('after'));
$config->addComponent($export = new GridFieldExportButton('buttons-after-left'));
$config->addComponent($print = new GridFieldPrintButton('buttons-after-left'));
Expand Down

0 comments on commit 75bec8c

Please sign in to comment.