diff --git a/code/controllers/AssetAdmin.php b/code/controllers/AssetAdmin.php index 7f78b806..464a80d2 100644 --- a/code/controllers/AssetAdmin.php +++ b/code/controllers/AssetAdmin.php @@ -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()); diff --git a/code/controllers/CMSMain.php b/code/controllers/CMSMain.php index 2ea1341b..f4717139 100644 --- a/code/controllers/CMSMain.php +++ b/code/controllers/CMSMain.php @@ -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); diff --git a/code/controllers/CMSPageAddController.php b/code/controllers/CMSPageAddController.php index 1beb6276..9a342ae0 100644 --- a/code/controllers/CMSPageAddController.php +++ b/code/controllers/CMSPageAddController.php @@ -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')); diff --git a/code/controllers/CMSPageHistoryController.php b/code/controllers/CMSPageHistoryController.php index 3503d1bd..7048765e 100644 --- a/code/controllers/CMSPageHistoryController.php +++ b/code/controllers/CMSPageHistoryController.php @@ -253,13 +253,13 @@ class CMSPageHistoryController extends CMSMain { // Use