IMPR: AJAX processing

This commit is contained in:
Tony Air 2024-02-02 23:24:58 +02:00
parent cd78ad1f9e
commit 53a1b8ff35
3 changed files with 46 additions and 1 deletions

View File

@ -45,6 +45,8 @@ SilverStripe\Core\Injector\Injector:
class: A2nt\CMSNiceties\Forms\GridField\GridFieldConfig_RecordEditor class: A2nt\CMSNiceties\Forms\GridField\GridFieldConfig_RecordEditor
SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor: SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor:
class: A2nt\CMSNiceties\Forms\GridField\GridFieldConfig_RelationEditor class: A2nt\CMSNiceties\Forms\GridField\GridFieldConfig_RelationEditor
SilverStripe\Forms\FormRequestHandler:
class: A2nt\CMSNiceties\Ajax\AjaxFormRequestHandler
SilverStripe\UserForms\Form\UserForm: SilverStripe\UserForms\Form\UserForm:
extensions: extensions:

View 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;
}
}

View File

@ -4,7 +4,6 @@ namespace A2nt\CMSNiceties\Ajax\Ex;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Extension; use SilverStripe\Core\Extension;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\Form; use SilverStripe\Forms\Form;