mirror of
https://github.com/a2nt/cms-niceties.git
synced 2024-10-22 11:05:46 +02:00
IMPR: AJAX processing
This commit is contained in:
parent
cd78ad1f9e
commit
53a1b8ff35
@ -45,6 +45,8 @@ SilverStripe\Core\Injector\Injector:
|
||||
class: A2nt\CMSNiceties\Forms\GridField\GridFieldConfig_RecordEditor
|
||||
SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor:
|
||||
class: A2nt\CMSNiceties\Forms\GridField\GridFieldConfig_RelationEditor
|
||||
SilverStripe\Forms\FormRequestHandler:
|
||||
class: A2nt\CMSNiceties\Ajax\AjaxFormRequestHandler
|
||||
|
||||
SilverStripe\UserForms\Form\UserForm:
|
||||
extensions:
|
||||
|
44
src/Ajax/AjaxFormRequestHandler.php
Normal file
44
src/Ajax/AjaxFormRequestHandler.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace A2nt\CMSNiceties\Ajax;
|
||||
|
||||
use A2nt\CMSNiceties\Ajax\Ex\AjaxControllerEx;
|
||||
use SilverStripe\Forms\FormRequestHandler;
|
||||
use SilverStripe\ORM\ValidationResult;
|
||||
|
||||
class AjaxFormRequestHandler extends FormRequestHandler
|
||||
{
|
||||
private static $allowed_actions = [
|
||||
'httpSubmission',
|
||||
];
|
||||
|
||||
/**
|
||||
* Handle a form submission. GET and POST requests behave identically.
|
||||
* Populates the form with {@link loadDataFrom()}, calls {@link validate()},
|
||||
* and only triggers the requested form action/method
|
||||
* if the form is valid.
|
||||
*
|
||||
* @param HTTPRequest $request
|
||||
* @return HTTPResponse
|
||||
* @throws HTTPResponse_Exception
|
||||
*/
|
||||
public function httpSubmission($request)
|
||||
{
|
||||
$resp = parent::httpSubmission($request);
|
||||
|
||||
if (!AjaxControllerEx::isFormRequest()) {
|
||||
return $resp;
|
||||
}
|
||||
|
||||
$validation = $this->form->validationResult();
|
||||
if (!$validation->isValid()) {
|
||||
$messages = $validation->getMessages();
|
||||
return json_encode([
|
||||
'status' => ValidationResult::TYPE_ERROR,
|
||||
'msgs' => $messages,
|
||||
]);
|
||||
}
|
||||
|
||||
return $resp;
|
||||
}
|
||||
}
|
@ -4,7 +4,6 @@ namespace A2nt\CMSNiceties\Ajax\Ex;
|
||||
|
||||
use SilverStripe\Control\Controller;
|
||||
use SilverStripe\Control\Director;
|
||||
use SilverStripe\Control\HTTPRequest;
|
||||
use SilverStripe\Core\Extension;
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
use SilverStripe\Forms\Form;
|
||||
|
Loading…
Reference in New Issue
Block a user