mirror of
https://github.com/silverstripe/silverstripe-cms
synced 2024-10-22 08:05:56 +02:00
API Update to use new form submission handling (#1691)
This commit is contained in:
parent
981ca17695
commit
bf58c5aef4
@ -49,6 +49,7 @@ use SilverStripe\ORM\DB;
|
|||||||
use SilverStripe\ORM\FieldType\DBHTMLText;
|
use SilverStripe\ORM\FieldType\DBHTMLText;
|
||||||
use SilverStripe\ORM\HiddenClass;
|
use SilverStripe\ORM\HiddenClass;
|
||||||
use SilverStripe\ORM\SS_List;
|
use SilverStripe\ORM\SS_List;
|
||||||
|
use SilverStripe\ORM\ValidationResult;
|
||||||
use SilverStripe\ORM\Versioning\Versioned;
|
use SilverStripe\ORM\Versioning\Versioned;
|
||||||
use SilverStripe\Security\Member;
|
use SilverStripe\Security\Member;
|
||||||
use SilverStripe\Security\Permission;
|
use SilverStripe\Security\Permission;
|
||||||
@ -983,12 +984,10 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
|
|||||||
new FieldList()
|
new FieldList()
|
||||||
)->setHTMLID('Form_ListViewForm');
|
)->setHTMLID('Form_ListViewForm');
|
||||||
$listview->setAttribute('data-pjax-fragment', 'ListViewForm');
|
$listview->setAttribute('data-pjax-fragment', 'ListViewForm');
|
||||||
$listview->setValidationResponseCallback(function() use ($negotiator, $listview) {
|
$listview->setValidationResponseCallback(function(ValidationResult $errors) use ($negotiator, $listview) {
|
||||||
$request = $this->getRequest();
|
$request = $this->getRequest();
|
||||||
if($request->isAjax() && $negotiator) {
|
if($request->isAjax() && $negotiator) {
|
||||||
$listview->setupFormErrors();
|
|
||||||
$result = $listview->forTemplate();
|
$result = $listview->forTemplate();
|
||||||
|
|
||||||
return $negotiator->respond($request, array(
|
return $negotiator->respond($request, array(
|
||||||
'CurrentForm' => function() use($result) {
|
'CurrentForm' => function() use($result) {
|
||||||
return $result;
|
return $result;
|
||||||
|
@ -17,6 +17,7 @@ use SilverStripe\Forms\TreeDropdownField;
|
|||||||
use SilverStripe\ORM\DataObject;
|
use SilverStripe\ORM\DataObject;
|
||||||
use SilverStripe\ORM\FieldType\DBField;
|
use SilverStripe\ORM\FieldType\DBField;
|
||||||
use SilverStripe\ORM\ValidationException;
|
use SilverStripe\ORM\ValidationException;
|
||||||
|
use SilverStripe\ORM\ValidationResult;
|
||||||
use SilverStripe\Security\Member;
|
use SilverStripe\Security\Member;
|
||||||
use SilverStripe\Security\Security;
|
use SilverStripe\Security\Security;
|
||||||
|
|
||||||
@ -146,12 +147,10 @@ class CMSPageAddController extends CMSPageEditController {
|
|||||||
)->setHTMLID('Form_AddForm');
|
)->setHTMLID('Form_AddForm');
|
||||||
$form->setAttribute('data-hints', $this->SiteTreeHints());
|
$form->setAttribute('data-hints', $this->SiteTreeHints());
|
||||||
$form->setAttribute('data-childfilter', $this->Link('childfilter'));
|
$form->setAttribute('data-childfilter', $this->Link('childfilter'));
|
||||||
$form->setValidationResponseCallback(function() use ($negotiator, $form) {
|
$form->setValidationResponseCallback(function(ValidationResult $errors) use ($negotiator, $form) {
|
||||||
$request = $this->getRequest();
|
$request = $this->getRequest();
|
||||||
if($request->isAjax() && $negotiator) {
|
if($request->isAjax() && $negotiator) {
|
||||||
$form->setupFormErrors();
|
|
||||||
$result = $form->forTemplate();
|
$result = $form->forTemplate();
|
||||||
|
|
||||||
return $negotiator->respond($request, array(
|
return $negotiator->respond($request, array(
|
||||||
'CurrentForm' => function() use($result) {
|
'CurrentForm' => function() use($result) {
|
||||||
return $result;
|
return $result;
|
||||||
@ -198,13 +197,7 @@ class CMSPageAddController extends CMSPageEditController {
|
|||||||
|
|
||||||
$record = $this->getNewItem("new-$className-$parentID".$suffix, false);
|
$record = $this->getNewItem("new-$className-$parentID".$suffix, false);
|
||||||
$this->extend('updateDoAdd', $record, $form);
|
$this->extend('updateDoAdd', $record, $form);
|
||||||
|
$record->write();
|
||||||
try {
|
|
||||||
$record->write();
|
|
||||||
} catch(ValidationException $ex) {
|
|
||||||
$form->sessionMessage($ex->getResult()->message(), 'bad');
|
|
||||||
return $this->getResponseNegotiator()->respond($this->getRequest());
|
|
||||||
}
|
|
||||||
|
|
||||||
$editController = CMSPageEditController::singleton();
|
$editController = CMSPageEditController::singleton();
|
||||||
$editController->setCurrentPageID($record->ID);
|
$editController->setCurrentPageID($record->ID);
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace SilverStripe\CMS\Controllers;
|
namespace SilverStripe\CMS\Controllers;
|
||||||
|
|
||||||
use SilverStripe\Admin\AddToCampaignHandler;
|
use SilverStripe\Admin\AddToCampaignHandler;
|
||||||
|
use SilverStripe\Control\Controller;
|
||||||
use SilverStripe\Control\HTTPRequest;
|
use SilverStripe\Control\HTTPRequest;
|
||||||
use SilverStripe\Control\HTTPResponse;
|
use SilverStripe\Control\HTTPResponse;
|
||||||
use SilverStripe\Core\Convert;
|
use SilverStripe\Core\Convert;
|
||||||
@ -54,15 +55,14 @@ class CMSPageEditController extends CMSMain {
|
|||||||
if (is_null($results)) {
|
if (is_null($results)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$request = $this->getRequest();
|
|
||||||
if($request->getHeader('X-Formschema-Request')) {
|
|
||||||
$data = $this->getSchemaForForm($handler->Form($record));
|
|
||||||
$data['message'] = $results;
|
|
||||||
|
|
||||||
$response = new HTTPResponse(Convert::raw2json($data));
|
if($this->getSchemaRequested()) {
|
||||||
$response->addHeader('Content-Type', 'application/json');
|
// Send extra "message" data with schema response
|
||||||
return $response;
|
$extraData = ['message' => $results];
|
||||||
|
$schemaId = Controller::join_links($this->Link('schema/AddToCampaignForm'), $id);
|
||||||
|
return $this->getSchemaResponse($schemaId, $form, null, $extraData);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,6 +44,7 @@ use SilverStripe\ORM\DB;
|
|||||||
use SilverStripe\ORM\HiddenClass;
|
use SilverStripe\ORM\HiddenClass;
|
||||||
use SilverStripe\ORM\Hierarchy\Hierarchy;
|
use SilverStripe\ORM\Hierarchy\Hierarchy;
|
||||||
use SilverStripe\ORM\ManyManyList;
|
use SilverStripe\ORM\ManyManyList;
|
||||||
|
use SilverStripe\ORM\ValidationResult;
|
||||||
use SilverStripe\ORM\Versioning\Versioned;
|
use SilverStripe\ORM\Versioning\Versioned;
|
||||||
use SilverStripe\Security\Group;
|
use SilverStripe\Security\Group;
|
||||||
use SilverStripe\Security\Member;
|
use SilverStripe\Security\Member;
|
||||||
@ -1661,12 +1662,13 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
|
|||||||
? $this->CopyContentFrom()
|
? $this->CopyContentFrom()
|
||||||
: $this;
|
: $this;
|
||||||
if(!in_array($subject->ClassName, $allowed)) {
|
if(!in_array($subject->ClassName, $allowed)) {
|
||||||
$result->error(
|
$result->addError(
|
||||||
_t(
|
_t(
|
||||||
'SiteTree.PageTypeNotAllowed',
|
'SiteTree.PageTypeNotAllowed',
|
||||||
'Page type "{type}" not allowed as child of this parent page',
|
'Page type "{type}" not allowed as child of this parent page',
|
||||||
array('type' => $subject->i18n_singular_name())
|
array('type' => $subject->i18n_singular_name())
|
||||||
),
|
),
|
||||||
|
ValidationResult::TYPE_ERROR,
|
||||||
'ALLOWED_CHILDREN'
|
'ALLOWED_CHILDREN'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -1674,12 +1676,13 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
|
|||||||
|
|
||||||
// "Can be root" validation
|
// "Can be root" validation
|
||||||
if(!$this->stat('can_be_root') && !$this->ParentID) {
|
if(!$this->stat('can_be_root') && !$this->ParentID) {
|
||||||
$result->error(
|
$result->addError(
|
||||||
_t(
|
_t(
|
||||||
'SiteTree.PageTypNotAllowedOnRoot',
|
'SiteTree.PageTypNotAllowedOnRoot',
|
||||||
'Page type "{type}" is not allowed on the root level',
|
'Page type "{type}" is not allowed on the root level',
|
||||||
array('type' => $this->i18n_singular_name())
|
array('type' => $this->i18n_singular_name())
|
||||||
),
|
),
|
||||||
|
ValidationResult::TYPE_ERROR,
|
||||||
'CAN_BE_ROOT'
|
'CAN_BE_ROOT'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ use SilverStripe\Forms\LiteralField;
|
|||||||
use SilverStripe\Forms\ReadonlyTransformation;
|
use SilverStripe\Forms\ReadonlyTransformation;
|
||||||
use SilverStripe\Forms\TreeDropdownField;
|
use SilverStripe\Forms\TreeDropdownField;
|
||||||
use SilverStripe\ORM\DataObject;
|
use SilverStripe\ORM\DataObject;
|
||||||
|
use SilverStripe\ORM\ValidationResult;
|
||||||
use SilverStripe\ORM\Versioning\Versioned;
|
use SilverStripe\ORM\Versioning\Versioned;
|
||||||
use SilverStripe\Security\Member;
|
use SilverStripe\Security\Member;
|
||||||
use Page;
|
use Page;
|
||||||
@ -305,12 +306,13 @@ class VirtualPage extends Page {
|
|||||||
// "Can be root" validation
|
// "Can be root" validation
|
||||||
$orig = $this->CopyContentFrom();
|
$orig = $this->CopyContentFrom();
|
||||||
if($orig && $orig->exists() && !$orig->stat('can_be_root') && !$this->ParentID) {
|
if($orig && $orig->exists() && !$orig->stat('can_be_root') && !$this->ParentID) {
|
||||||
$result->error(
|
$result->addError(
|
||||||
_t(
|
_t(
|
||||||
'VirtualPage.PageTypNotAllowedOnRoot',
|
'VirtualPage.PageTypNotAllowedOnRoot',
|
||||||
'Original page type "{type}" is not allowed on the root level for this virtual page',
|
'Original page type "{type}" is not allowed on the root level for this virtual page',
|
||||||
array('type' => $orig->i18n_singular_name())
|
array('type' => $orig->i18n_singular_name())
|
||||||
),
|
),
|
||||||
|
ValidationResult::TYPE_ERROR,
|
||||||
'CAN_BE_ROOT_VIRTUAL'
|
'CAN_BE_ROOT_VIRTUAL'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1057,27 +1057,27 @@ class SiteTreeTest extends SapphireTest {
|
|||||||
|
|
||||||
$classB->ParentID = $page->ID;
|
$classB->ParentID = $page->ID;
|
||||||
$valid = $classB->doValidate();
|
$valid = $classB->doValidate();
|
||||||
$this->assertTrue($valid->valid(), "Does allow children on unrestricted parent");
|
$this->assertTrue($valid->isValid(), "Does allow children on unrestricted parent");
|
||||||
|
|
||||||
$classB->ParentID = $classA->ID;
|
$classB->ParentID = $classA->ID;
|
||||||
$valid = $classB->doValidate();
|
$valid = $classB->doValidate();
|
||||||
$this->assertTrue($valid->valid(), "Does allow child specifically allowed by parent");
|
$this->assertTrue($valid->isValid(), "Does allow child specifically allowed by parent");
|
||||||
|
|
||||||
$classC->ParentID = $classA->ID;
|
$classC->ParentID = $classA->ID;
|
||||||
$valid = $classC->doValidate();
|
$valid = $classC->doValidate();
|
||||||
$this->assertFalse($valid->valid(), "Doesnt allow child on parents specifically restricting children");
|
$this->assertFalse($valid->isValid(), "Doesnt allow child on parents specifically restricting children");
|
||||||
|
|
||||||
$classB->ParentID = $classC->ID;
|
$classB->ParentID = $classC->ID;
|
||||||
$valid = $classB->doValidate();
|
$valid = $classB->doValidate();
|
||||||
$this->assertFalse($valid->valid(), "Doesnt allow child on parents disallowing all children");
|
$this->assertFalse($valid->isValid(), "Doesnt allow child on parents disallowing all children");
|
||||||
|
|
||||||
$classB->ParentID = $classCext->ID;
|
$classB->ParentID = $classCext->ID;
|
||||||
$valid = $classB->doValidate();
|
$valid = $classB->doValidate();
|
||||||
$this->assertTrue($valid->valid(), "Extensions of allowed classes are incorrectly reported as invalid");
|
$this->assertTrue($valid->isValid(), "Extensions of allowed classes are incorrectly reported as invalid");
|
||||||
|
|
||||||
$classCext->ParentID = $classD->ID;
|
$classCext->ParentID = $classD->ID;
|
||||||
$valid = $classCext->doValidate();
|
$valid = $classCext->doValidate();
|
||||||
$this->assertFalse($valid->valid(), "Doesnt allow child where only parent class is allowed on parent node, and asterisk prefixing is used");
|
$this->assertFalse($valid->isValid(), "Doesnt allow child where only parent class is allowed on parent node, and asterisk prefixing is used");
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testClassDropdown() {
|
public function testClassDropdown() {
|
||||||
|
@ -400,11 +400,11 @@ class VirtualPageTest extends FunctionalTest {
|
|||||||
|
|
||||||
$classBVirtual->ParentID = $classA->ID;
|
$classBVirtual->ParentID = $classA->ID;
|
||||||
$valid = $classBVirtual->doValidate();
|
$valid = $classBVirtual->doValidate();
|
||||||
$this->assertTrue($valid->valid(), "Does allow child linked to virtual page type allowed by parent");
|
$this->assertTrue($valid->isValid(), "Does allow child linked to virtual page type allowed by parent");
|
||||||
|
|
||||||
$classCVirtual->ParentID = $classA->ID;
|
$classCVirtual->ParentID = $classA->ID;
|
||||||
$valid = $classCVirtual->doValidate();
|
$valid = $classCVirtual->doValidate();
|
||||||
$this->assertFalse($valid->valid(), "Doesn't allow child linked to virtual page type disallowed by parent");
|
$this->assertFalse($valid->isValid(), "Doesn't allow child linked to virtual page type disallowed by parent");
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetVirtualFields() {
|
public function testGetVirtualFields() {
|
||||||
|
Loading…
Reference in New Issue
Block a user