Skip to content

Commit

Permalink
Replace CMSForm use with new setValidationResponseCallback() API
Browse files Browse the repository at this point in the history
Preparing for form schema API, see silverstripe/silverstripe-framework#4938
  • Loading branch information
chillu committed Mar 1, 2016
1 parent 99c72fc commit 84699a8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 23 deletions.
17 changes: 15 additions & 2 deletions code/controllers/AssetAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,8 @@ public function SearchForm() {
}

public function AddForm() {
$form = CMSForm::create(
$negotiator = $this->getResponseNegotiator();
$form = Form::create(
$this,
'AddForm',
new FieldList(
Expand All @@ -430,7 +431,19 @@ public function AddForm() {
->setTitle(_t('AssetAdmin.ActionAdd', 'Add folder'))
)
)->setHTMLID('Form_AddForm');
$form->setResponseNegotiator($this->getResponseNegotiator());
$form->setValidationResponseCallback(function() use ($negotiator, $form) {
$request = $this->getRequest();
if($request->isAjax() && $negotiator) {
$form->setupFormErrors();
$result = $form->forTemplate();

return $negotiator->respond($request, array(
'CurrentForm' => function() use($result) {
return $result;
}
));
}
});
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
// TODO Can't merge $FormAttributes in template at the moment
$form->addExtraClass('add-form cms-add-form cms-edit-form cms-panel-padded center ' . $this->BaseCSSClasses());
Expand Down
32 changes: 17 additions & 15 deletions code/controllers/CMSMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ public function getRecord($id, $versionID = null) {
/**
* @param int $id
* @param FieldList $fields
* @return CMSForm
* @return Form
*/
public function getEditForm($id = null, $fields = null) {
if(!$id) $id = $this->currentPageID();
Expand Down Expand Up @@ -656,18 +656,8 @@ public function getEditForm($id = null, $fields = null) {
$validator = new RequiredFields();
}

$form = CMSForm::create(
$this, "EditForm", $fields, $actions, $validator
)->setHTMLID('Form_EditForm');
$form->setResponseNegotiator($this->getResponseNegotiator());
$form->loadDataFrom($record);
$form->disableDefaultAction();
$form->addExtraClass('cms-edit-form');
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
// TODO Can't merge $FormAttributes in template at the moment
$form->addExtraClass('center ' . $this->BaseCSSClasses());
// if($form->Fields()->hasTabset()) $form->Fields()->findOrMakeTab('Root')->setTemplate('CMSTabSet');
$form->setAttribute('data-pjax-fragment', 'CurrentForm');
// Set validation exemptions for specific actions
$form->setValidationExemptActions(array('restore', 'revert', 'deletefromlive', 'delete', 'unpublish', 'rollback', 'doRollback'));

Expand All @@ -684,10 +674,9 @@ public function getEditForm($id = null, $fields = null) {
$this->extend('updateEditForm', $form);
return $form;
} else if($id) {
$form = CMSForm::create( $this, "EditForm", new FieldList(
$form = Form::create( $this, "EditForm", new FieldList(
new LabelField('PageDoesntExistLabel',_t('CMSMain.PAGENOTEXISTS',"This page doesn't exist"))), new FieldList()
)->setHTMLID('Form_EditForm');
$form->setResponseNegotiator($this->getResponseNegotiator());
return $form;
}
}
Expand Down Expand Up @@ -842,14 +831,27 @@ public function ListViewForm() {
}
));

$listview = CMSForm::create(
$negotiator = $this->getResponseNegotiator();
$listview = Form::create(
$this,
'ListViewForm',
new FieldList($gridField),
new FieldList()
)->setHTMLID('Form_ListViewForm');
$listview->setAttribute('data-pjax-fragment', 'ListViewForm');
$listview->setResponseNegotiator($this->getResponseNegotiator());
$listview->setValidationResponseCallback(function() use ($negotiator, $listview) {
$request = $this->getRequest();
if($request->isAjax() && $negotiator) {
$listview->setupFormErrors();
$result = $listview->forTemplate();

return $negotiator->respond($request, array(
'CurrentForm' => function() use($result) {
return $result;
}
));
}
});

$this->extend('updateListView', $listview);

Expand Down
20 changes: 16 additions & 4 deletions code/controllers/CMSPageAddController.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,26 @@ public function AddForm() {
);

$this->extend('updatePageOptions', $fields);

$form = CMSForm::create(

$negotiator = $this->getResponseNegotiator();
$form = Form::create(
$this, "AddForm", $fields, $actions
)->setHTMLID('Form_AddForm');
$form->setAttribute('data-hints', $this->SiteTreeHints());
$form->setAttribute('data-childfilter', $this->Link('childfilter'));

$form->setResponseNegotiator($this->getResponseNegotiator());
$form->setValidationResponseCallback(function() use ($negotiator, $form) {
$request = $this->getRequest();
if($request->isAjax() && $negotiator) {
$this->setupFormErrors();
$result = $this->forTemplate();

return $negotiator->respond($request, array(
'CurrentForm' => function() use($result) {
return $result;
}
));
}
});
$form->addExtraClass('cms-add-form stacked cms-content center cms-edit-form ' . $this->BaseCSSClasses());
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));

Expand Down
4 changes: 2 additions & 2 deletions code/controllers/CMSPageHistoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,13 @@ public function VersionsForm() {
// Use <button> to allow full jQuery UI styling
foreach($actions->dataFields() as $action) $action->setUseButtonTag(true);

$form = CMSForm::create(
$negotiator = $this->getResponseNegotiator();
$form = Form::create(
$this,
'VersionsForm',
$fields,
$actions
)->setHTMLID('Form_VersionsForm');
$form->setResponseNegotiator($this->getResponseNegotiator());
$form->loadDataFrom($this->getRequest()->requestVars());
$hiddenID->setValue($id);
$form->unsetValidator();
Expand Down

0 comments on commit 84699a8

Please sign in to comment.