Compare commits

..

4 Commits

Author SHA1 Message Date
0588e3a409 IMPR: SS5 test 2024-03-12 03:45:38 +02:00
6a52f247d1 IMPR: SS5 test 2024-03-12 03:36:47 +02:00
f2652b48c3 IMPR: Minor fixtures 2024-03-12 01:32:50 +02:00
d65c85a192 IMPR: Ajax form and redirect processing 2024-03-12 01:11:26 +02:00
2 changed files with 50 additions and 3 deletions

View File

@ -13,10 +13,9 @@
}], }],
"minimum-stability": "dev", "minimum-stability": "dev",
"require": { "require": {
"silverstripe/cms": "^4", "silverstripe/cms": "*",
"a2nt/silverstripe-elemental-basics": "*", "a2nt/silverstripe-elemental-basics": "*",
"silverstripe/widgets": "*", "silverstripe/widgets": "*",
"gorriecoe/silverstripe-embed": "*",
"gorriecoe/silverstripe-linkfield": "*" "gorriecoe/silverstripe-linkfield": "*"
}, },
"autoload": { "autoload": {

View File

@ -4,11 +4,14 @@ 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\Control\HTTPResponse;
use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Config;
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;
use SilverStripe\ORM\FieldType\DBHTMLText; use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\ORM\ValidationResult;
use SilverStripe\Security\MemberAuthenticator\MemberAuthenticator; use SilverStripe\Security\MemberAuthenticator\MemberAuthenticator;
use SilverStripe\Security\Security; use SilverStripe\Security\Security;
use SilverStripe\View\SSViewer; use SilverStripe\View\SSViewer;
@ -194,7 +197,7 @@ class AjaxControllerEx extends Extension
return $json && $str != $json; return $json && $str != $json;
} }
public function prepareAjaxResponse($response) public function prepareAjaxResponse(HTTPResponse $response)
{ {
$ctrl = $this->owner; $ctrl = $this->owner;
@ -210,7 +213,52 @@ class AjaxControllerEx extends Extension
); );
$body = $response->getBody(); $body = $response->getBody();
if (self::isFormRequest()) {
/* @var $req \SilverStripe\Control\HTTPRequest */
$req = $ctrl->getRequest();
$sess = $req->getSession();
$formName = $req->requestVar('formid');
$data = $sess->get('FormInfo');
// form validation response
if ($formName && $data && isset($data[$formName]['result'])) {
$sess->clear('FormInfo.'.$formName);
/* @var $valid \SilverStripe\ORM\ValidationResult */
$valid = unserialize($data[$formName]['result']);
$msgs = $valid->getMessages();
$body = json_encode([
'status' => $valid->isValid()
? ValidationResult::TYPE_GOOD
: ValidationResult::TYPE_ERROR,
'msgs' => $msgs,
]);
$response->removeHeader('Location');
$response->setStatusCode(200);
$response->setBody($body);
return $response;
}
}
// ajax redirect
if ($response->isRedirect()) {
$body = json_encode([
'location' => $response->getHeader('location'),
'loadAjax' => true,
]);
$response->removeHeader('Location');
$response->setStatusCode(200);
$response->setBody($body);
return $response;
}
// render page
if (!self::isJson($body)) { if (!self::isJson($body)) {
$body = json_encode([ $body = json_encode([
'ID' => $record->ID, 'ID' => $record->ID,