Skip to content

Commit

Permalink
ENH Return PJAX responses from gridfield edit forms (#11206)
Browse files Browse the repository at this point in the history
undefined
  • Loading branch information
GuySartorelli committed Apr 26, 2024
1 parent 19ea32e commit 142a318
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/Control/RequestHandler.php
Expand Up @@ -678,4 +678,15 @@ public function redirectBack(): HTTPResponse
$url = Director::absoluteURL((string) $url);
return $this->redirect($url);
}

/**
* Convert an array of data to JSON and wrap it in an HTML tag as pjax is used and jQuery will parse this
* as an element on the client side in LeftAndMain.js handleAjaxResponse()
* The attribute type="application/json" denotes this is a data block and won't be processed by a browser
* https://html.spec.whatwg.org/#the-script-element
*/
protected function prepareDataForPjax(array $data): string
{
return '<script type="application/json">' . json_encode($data) . '</script>';
}
}
52 changes: 51 additions & 1 deletion src/Forms/GridField/GridFieldDetailForm_ItemRequest.php
Expand Up @@ -7,6 +7,7 @@
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\PjaxResponseNegotiator;
use SilverStripe\Control\RequestHandler;
use SilverStripe\Core\Convert;
use SilverStripe\Core\ClassInfo;
Expand Down Expand Up @@ -170,7 +171,7 @@ public function edit($request)
])->renderWith($this->getTemplates());

if ($request->isAjax()) {
return $return;
return $this->getResponseNegotiator($return)->respond($request);
} else {
// If not requested by ajax, we need to render it within the controller context+template
return $controller->customise([
Expand Down Expand Up @@ -298,6 +299,23 @@ public function ItemEditForm()
}

$form->Backlink = $this->getBackLink();

// Ensure the correct validation response is returned for AJAX requests
$form->setValidationResponseCallback(function (ValidationResult $errors) use ($form) {
$request = $this->getRequest();
if (!$request->isAjax()) {
return null;
}
$negotiator = $this->getResponseNegotiator($form->forTemplate());
return $negotiator->respond($request, [
'ValidationResult' => function () use ($errors) {
return $this->prepareDataForPjax([
'isValid' => $errors->isValid(),
'messages' => $errors->getMessages()
]);
}
]);
});
}

$cb = $this->component->getItemEditFormCallback();
Expand Down Expand Up @@ -945,4 +963,36 @@ private function getModelName(): string
}
return ClassInfo::shortName($this->record);
}

/**
* Get Pjax response negotiator so form submission mirrors other form submission in the CMS.
* See LeftAndMain::getResponseNegotiator()
*/
private function getResponseNegotiator(DBHTMLText $renderedForm): PjaxResponseNegotiator
{
return new PjaxResponseNegotiator([
'default' => function () use ($renderedForm) {
return $renderedForm;
},
'Content' => function () use ($renderedForm) {
return $renderedForm;
},
'CurrentForm' => function () use ($renderedForm) {
return $renderedForm;
},
'Breadcrumbs' => function () {
return $this->renderWith([
'type' => 'Includes',
'SilverStripe\\Admin\\CMSBreadcrumbs'
]);
},
'ValidationResult' => function () {
// Assume valid by default, mirroring LeftAndMain's response negotiator
return $this->prepareDataForPjax([
'isValid' => true,
'messages' => '',
]);
}
], $this->getToplevelController()->getResponse());
}
}

0 comments on commit 142a318

Please sign in to comment.