Skip to content

Commit

Permalink
API Update to use new form submission handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian Mooyman committed Dec 8, 2016
1 parent 669a7e6 commit 5c3c56c
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 32 deletions.
5 changes: 2 additions & 3 deletions code/Controllers/CMSMain.php
Expand Up @@ -49,6 +49,7 @@
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;
Expand Down Expand Up @@ -983,12 +984,10 @@ public function ListViewForm() {
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;
Expand Down
13 changes: 3 additions & 10 deletions code/Controllers/CMSPageAddController.php
Expand Up @@ -17,6 +17,7 @@
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;

Expand Down Expand Up @@ -146,12 +147,10 @@ public function AddForm() {
)->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;
Expand Down Expand Up @@ -198,13 +197,7 @@ public function doAdd($data, $form) {

$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);
Expand Down
16 changes: 8 additions & 8 deletions code/Controllers/CMSPageEditController.php
Expand Up @@ -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;
Expand Down Expand Up @@ -54,15 +55,14 @@ public function addtocampaign($data, $form)
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;
}

Expand Down
7 changes: 5 additions & 2 deletions code/Model/SiteTree.php
Expand Up @@ -44,6 +44,7 @@
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;
Expand Down Expand Up @@ -1661,25 +1662,27 @@ public function validate() {
? $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'
);
}
}

// "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'
);
}
Expand Down
4 changes: 3 additions & 1 deletion code/Model/VirtualPage.php
Expand Up @@ -7,6 +7,7 @@
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;
Expand Down Expand Up @@ -305,12 +306,13 @@ public function validate() {
// "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'
);
}
Expand Down
12 changes: 6 additions & 6 deletions tests/model/SiteTreeTest.php
Expand Up @@ -1057,27 +1057,27 @@ public function testAllowedChildrenValidation() {

$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() {
Expand Down
4 changes: 2 additions & 2 deletions tests/model/VirtualPageTest.php
Expand Up @@ -400,11 +400,11 @@ public function testAllowedChildrenLimitedOnVirtualPages() {

$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() {
Expand Down

0 comments on commit 5c3c56c

Please sign in to comment.