Skip to content

Commit

Permalink
ENH Pass form validation result to client
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Aug 27, 2021
1 parent 42a0a92 commit 18f9e12
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions client/src/legacy/LeftAndMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,13 +492,12 @@ $.entwine('ss', function($) {
// Save tab selections so we can restore them later
this.saveTabState();


// Standard Pjax behaviour is to replace the submitted form with new content.
// The returned view isn't always decided upon when the request
// is fired, so the server might decide to change it based on its own logic,
// sending back different `X-Pjax` headers and content
jQuery.ajax(jQuery.extend({
headers: {"X-Pjax" : "CurrentForm,Breadcrumbs"},
headers: {"X-Pjax" : "CurrentForm,Breadcrumbs,ValidationResult"},
url: form.attr('action'),
data: formData,
type: 'POST',
Expand Down
30 changes: 28 additions & 2 deletions code/LeftAndMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,12 @@ public function getResponseNegotiator()
'SilverStripe\\Admin\\CMSBreadcrumbs'
]);
},
'ValidationResult' => function () {
return $this->prepareDataForPjax([
'isValid' => true,
'messages' => []
]);
},
'default' => function () {
return $this->renderWith($this->getViewer('show'));
}
Expand Down Expand Up @@ -1438,6 +1444,12 @@ public function getEditForm($id = null, $fields = null)
return $negotiator->respond($request, array(
'CurrentForm' => function () use ($result) {
return $result;
},
'ValidationResult' => function () use ($errors) {
return $this->prepareDataForPjax([
'isValid' => $errors->isValid(),
'messages' => $errors->getMessages()
]);
}
));
}
Expand Down Expand Up @@ -1476,6 +1488,20 @@ public function getEditForm($id = null, $fields = null)
return $form;
}

/**
* Convert and arry 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
*
* @param array $data
* @return string
*/
private function prepareDataForPjax(array $data): string
{
return '<script type="application/json">' . json_encode($data) . '</script>';
}

/**
* Returns a placeholder form, used by {@link getEditForm()} if no record is selected.
* Our javascript logic always requires a form to be present in the CMS interface.
Expand Down Expand Up @@ -1658,11 +1684,11 @@ public function currentPageID()
if (isset($this->urlParams['ID']) && is_numeric($this->urlParams['ID'])) {
return $this->urlParams['ID'];
}

if (is_numeric($this->getRequest()->param('ID'))) {
return $this->getRequest()->param('ID');
}

/** @deprecated */
$session = $this->getRequest()->getSession();
return $session->get($this->sessionNamespace() . ".currentPage") ?: null;
Expand Down

0 comments on commit 18f9e12

Please sign in to comment.