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 26, 2021
1 parent 42a0a92 commit 966ee81
Show file tree
Hide file tree
Showing 3 changed files with 17 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
17 changes: 15 additions & 2 deletions code/LeftAndMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,10 @@ public function getResponseNegotiator()
'SilverStripe\\Admin\\CMSBreadcrumbs'
]);
},
'ValidationResult' => function () {
// Wrap JSON in <div> tags as jQuery will parse this as an element on the client side
return '<div>' . json_encode(['isValid' => true, 'messages' => []]) . '</div>';
},
'default' => function () {
return $this->renderWith($this->getViewer('show'));
}
Expand Down Expand Up @@ -1438,6 +1442,15 @@ public function getEditForm($id = null, $fields = null)
return $negotiator->respond($request, array(
'CurrentForm' => function () use ($result) {
return $result;
},
'ValidationResult' => function () use ($errors) {
// Wrap JSON in <div> tags as jQuery will parse this as an element on the client side
// TODO: if not get_class ValidationResult .. private method to share with declaration above etc
$data = [
'isValid' => $errors->isValid(),
'messages' => $errors->getMessages()
];
return '<div>' . json_encode($data) . '</div>';
}
));
}
Expand Down Expand Up @@ -1658,11 +1671,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 966ee81

Please sign in to comment.