API Update to use new form submission handling (#1691)

This commit is contained in:
Damian Mooyman 2016-12-09 16:00:46 +13:00 committed by Sam Minnée
parent 981ca17695
commit bf58c5aef4
7 changed files with 28 additions and 31 deletions

View File

@ -49,6 +49,7 @@ use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\ORM\HiddenClass;
use SilverStripe\ORM\SS_List;
use SilverStripe\ORM\ValidationResult;
use SilverStripe\ORM\Versioning\Versioned;
use SilverStripe\Security\Member;
use SilverStripe\Security\Permission;
@ -983,12 +984,10 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
new FieldList()
)->setHTMLID('Form_ListViewForm');
$listview->setAttribute('data-pjax-fragment', 'ListViewForm');
$listview->setValidationResponseCallback(function() use ($negotiator, $listview) {
$listview->setValidationResponseCallback(function(ValidationResult $errors) use ($negotiator, $listview) {
$request = $this->getRequest();
if($request->isAjax() && $negotiator) {
$listview->setupFormErrors();
$result = $listview->forTemplate();
return $negotiator->respond($request, array(
'CurrentForm' => function() use($result) {
return $result;

View File

@ -17,6 +17,7 @@ use SilverStripe\Forms\TreeDropdownField;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\ValidationException;
use SilverStripe\ORM\ValidationResult;
use SilverStripe\Security\Member;
use SilverStripe\Security\Security;
@ -146,12 +147,10 @@ class CMSPageAddController extends CMSPageEditController {
)->setHTMLID('Form_AddForm');
$form->setAttribute('data-hints', $this->SiteTreeHints());
$form->setAttribute('data-childfilter', $this->Link('childfilter'));
$form->setValidationResponseCallback(function() use ($negotiator, $form) {
$form->setValidationResponseCallback(function(ValidationResult $errors) use ($negotiator, $form) {
$request = $this->getRequest();
if($request->isAjax() && $negotiator) {
$form->setupFormErrors();
$result = $form->forTemplate();
return $negotiator->respond($request, array(
'CurrentForm' => function() use($result) {
return $result;
@ -198,13 +197,7 @@ class CMSPageAddController extends CMSPageEditController {
$record = $this->getNewItem("new-$className-$parentID".$suffix, false);
$this->extend('updateDoAdd', $record, $form);
try {
$record->write();
} catch(ValidationException $ex) {
$form->sessionMessage($ex->getResult()->message(), 'bad');
return $this->getResponseNegotiator()->respond($this->getRequest());
}
$record->write();
$editController = CMSPageEditController::singleton();
$editController->setCurrentPageID($record->ID);

View File

@ -3,6 +3,7 @@
namespace SilverStripe\CMS\Controllers;
use SilverStripe\Admin\AddToCampaignHandler;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Convert;
@ -54,15 +55,14 @@ class CMSPageEditController extends CMSMain {
if (is_null($results)) {
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));
$response->addHeader('Content-Type', 'application/json');
return $response;
if($this->getSchemaRequested()) {
// Send extra "message" data with schema response
$extraData = ['message' => $results];
$schemaId = Controller::join_links($this->Link('schema/AddToCampaignForm'), $id);
return $this->getSchemaResponse($schemaId, $form, null, $extraData);
}
return $results;
}

View File

@ -44,6 +44,7 @@ use SilverStripe\ORM\DB;
use SilverStripe\ORM\HiddenClass;
use SilverStripe\ORM\Hierarchy\Hierarchy;
use SilverStripe\ORM\ManyManyList;
use SilverStripe\ORM\ValidationResult;
use SilverStripe\ORM\Versioning\Versioned;
use SilverStripe\Security\Group;
use SilverStripe\Security\Member;
@ -1661,12 +1662,13 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
? $this->CopyContentFrom()
: $this;
if(!in_array($subject->ClassName, $allowed)) {
$result->error(
$result->addError(
_t(
'SiteTree.PageTypeNotAllowed',
'Page type "{type}" not allowed as child of this parent page',
array('type' => $subject->i18n_singular_name())
),
ValidationResult::TYPE_ERROR,
'ALLOWED_CHILDREN'
);
}
@ -1674,12 +1676,13 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
// "Can be root" validation
if(!$this->stat('can_be_root') && !$this->ParentID) {
$result->error(
$result->addError(
_t(
'SiteTree.PageTypNotAllowedOnRoot',
'Page type "{type}" is not allowed on the root level',
array('type' => $this->i18n_singular_name())
),
ValidationResult::TYPE_ERROR,
'CAN_BE_ROOT'
);
}

View File

@ -7,6 +7,7 @@ use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\ReadonlyTransformation;
use SilverStripe\Forms\TreeDropdownField;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ValidationResult;
use SilverStripe\ORM\Versioning\Versioned;
use SilverStripe\Security\Member;
use Page;
@ -305,12 +306,13 @@ class VirtualPage extends Page {
// "Can be root" validation
$orig = $this->CopyContentFrom();
if($orig && $orig->exists() && !$orig->stat('can_be_root') && !$this->ParentID) {
$result->error(
$result->addError(
_t(
'VirtualPage.PageTypNotAllowedOnRoot',
'Original page type "{type}" is not allowed on the root level for this virtual page',
array('type' => $orig->i18n_singular_name())
),
ValidationResult::TYPE_ERROR,
'CAN_BE_ROOT_VIRTUAL'
);
}

View File

@ -1057,27 +1057,27 @@ class SiteTreeTest extends SapphireTest {
$classB->ParentID = $page->ID;
$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;
$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;
$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;
$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;
$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;
$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() {

View File

@ -400,11 +400,11 @@ class VirtualPageTest extends FunctionalTest {
$classBVirtual->ParentID = $classA->ID;
$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;
$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() {