Merge pull request #1410 from open-sausages/features/form-schema

Form Schema
This commit is contained in:
Damian Mooyman 2016-03-08 10:55:20 +13:00
commit a737f9c901
5 changed files with 66 additions and 25 deletions

View File

@ -294,6 +294,20 @@ class AssetAdmin extends LeftAndMain implements PermissionProvider{
$form->setAttribute('data-pjax-fragment', 'CurrentForm');
$form->Fields()->findOrMakeTab('Root')->setTemplate('CMSTabSet');
// Optionally handle form submissions with 'X-Formschema-Request'
// which rely on having validation errors returned as structured data
$form->setValidationResponseCallback(function() use ($form) {
$request = $this->getRequest();
if($request->getHeader('X-Formschema-Request')) {
$data = $this->getSchemaForForm($form);
$response = new SS_HTTPResponse(Convert::raw2json($data));
$response->addHeader('Content-Type', 'application/json');
return $response;
}
});
$this->extend('updateEditForm', $form);
return $form;
@ -417,7 +431,8 @@ class AssetAdmin extends LeftAndMain implements PermissionProvider{
}
public function AddForm() {
$form = CMSForm::create(
$negotiator = $this->getResponseNegotiator();
$form = Form::create(
$this,
'AddForm',
new FieldList(
@ -430,7 +445,19 @@ class AssetAdmin extends LeftAndMain implements PermissionProvider{
->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());

View File

@ -588,7 +588,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
/**
* @param int $id
* @param FieldList $fields
* @return CMSForm
* @return Form
*/
public function getEditForm($id = null, $fields = null) {
if(!$id) $id = $this->currentPageID();
@ -656,18 +656,8 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
$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'));
@ -684,10 +674,9 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
$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;
}
}
@ -842,14 +831,27 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
}
));
$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);

View File

@ -113,14 +113,26 @@ class CMSPageAddController extends CMSPageEditController {
);
$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->setValidationResponseCallback(function() use ($negotiator, $form) {
$request = $this->getRequest();
if($request->isAjax() && $negotiator) {
$this->setupFormErrors();
$result = $this->forTemplate();
$form->setResponseNegotiator($this->getResponseNegotiator());
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'));

View File

@ -253,13 +253,13 @@ class CMSPageHistoryController extends CMSMain {
// 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();

View File

@ -522,7 +522,7 @@ class CMSMainTest extends FunctionalTest {
$page = $this->objFromFixture('Page', 'page1');
$controller = new CMSMain();
$form = $controller->getEditForm($page->ID);
$this->assertInstanceOf("CMSForm", $form);
$this->assertInstanceOf("Form", $form);
// Ensure that the form will not "validate" on delete or "unpublish" actions.
$exemptActions = $form->getValidationExemptActions();
@ -536,7 +536,7 @@ class CMSMainTest_ClassA extends Page implements TestOnly {
}
class CMSMainTest_ClassB extends Page implements TestOnly {
}
class CMSMainTest_NotRoot extends Page implements TestOnly {
@ -544,5 +544,5 @@ class CMSMainTest_NotRoot extends Page implements TestOnly {
}
class CMSMainTest_HiddenClass extends Page implements TestOnly, HiddenClass {
}