Skip to content

Commit

Permalink
Replace CMSForm use with new setValidationResponseCallback() API
Browse files Browse the repository at this point in the history
Preparing for form schema API, see silverstripe#4938
  • Loading branch information
chillu committed Mar 2, 2016
1 parent 404ac4a commit 746322a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 62 deletions.
52 changes: 0 additions & 52 deletions admin/code/CMSForm.php

This file was deleted.

24 changes: 19 additions & 5 deletions admin/code/LeftAndMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*
* This is essentially an abstract class which should be subclassed.
* See {@link CMSMain} for a good example.
*
* @property FormSchema $schema
*/
class LeftAndMain extends Controller implements PermissionProvider {

Expand Down Expand Up @@ -197,7 +199,7 @@ public function schema() {
}

// Make sure it's an AJAX GET request with a valid "X-Formschema-Request" header value.
if (!$req->isAjax() || !$req->isGET() || !count($schemaParts)) {
if (!$req->isGET() || !count($schemaParts)) {
throw new SS_HTTPResponse_Exception(
'Invalid request. Check you\'ve set a "X-Formschema-Request" header with "schema" or "state" values.',
400
Expand Down Expand Up @@ -1312,14 +1314,27 @@ public function getEditForm($id = null, $fields = null) {
$actionsFlattened = $actions->dataFields();
if($actionsFlattened) foreach($actionsFlattened as $action) $action->setUseButtonTag(true);

$form = CMSForm::create(
$negotiator = $this->getResponseNegotiator();
$form = Form::create(
$this, "EditForm", $fields, $actions
)->setHTMLID('Form_EditForm');
$form->setResponseNegotiator($this->getResponseNegotiator());
$form->addExtraClass('cms-edit-form');
$form->loadDataFrom($record);
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
$form->setAttribute('data-pjax-fragment', 'CurrentForm');
$form->setValidationResponseCallback(function() 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;
}
));
}
});

// Announce the capability so the frontend can decide whether to allow preview or not.
if(in_array('CMSPreviewable', class_implements($record))) {
Expand Down Expand Up @@ -1368,7 +1383,7 @@ public function getEditForm($id = null, $fields = null) {
* @return Form
*/
public function EmptyForm() {
$form = CMSForm::create(
$form = Form::create(
$this,
"EditForm",
new FieldList(
Expand All @@ -1387,7 +1402,6 @@ public function EmptyForm() {
),
new FieldList()
)->setHTMLID('Form_EditForm');
$form->setResponseNegotiator($this->getResponseNegotiator());
$form->unsetValidator();
$form->addExtraClass('cms-edit-form');
$form->addExtraClass('root-form');
Expand Down
3 changes: 1 addition & 2 deletions admin/code/ModelAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,12 @@ public function getEditForm($id = null, $fields = null) {
$listField->getConfig()->getComponentByType('GridFieldDetailForm')->setValidator($detailValidator);
}

$form = CMSForm::create(
$form = Form::create(
$this,
'EditForm',
new FieldList($listField),
new FieldList()
)->setHTMLID('Form_EditForm');
$form->setResponseNegotiator($this->getResponseNegotiator());
$form->addExtraClass('cms-edit-form cms-panel-padded center');
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
$editFormAction = Controller::join_links($this->Link($this->sanitiseClassName($this->modelClass)), 'EditForm');
Expand Down
3 changes: 1 addition & 2 deletions admin/code/SecurityAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,12 @@ public function getEditForm($id = null, $fields = null) {

$actions = new FieldList();

$form = CMSForm::create(
$form = Form::create(
$this,
'EditForm',
$fields,
$actions
)->setHTMLID('Form_EditForm');
$form->setResponseNegotiator($this->getResponseNegotiator());
$form->addExtraClass('cms-edit-form');
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
// Tab nav in CMS is rendered through separate template
Expand Down
36 changes: 35 additions & 1 deletion forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ class Form extends RequestHandler {
*/
protected $validator;

/**
* @var callable {@see setValidationResponseCallback()}
*/
protected $validationResponseCallback;

/**
* @var string
*/
Expand Down Expand Up @@ -479,16 +484,45 @@ public function checkAccessAction($action) {
);
}

/**
* @return callable
*/
public function getValidationResponseCallback() {
return $this->validationResponseCallback;
}

/**
* Overrules validation error behaviour in {@link httpSubmission()}
* when validation has failed. Useful for optional handling of a certain accepted content type.
*
* The callback can opt out of handling specific responses by returning NULL,
* in which case the default form behaviour will kick in.
*
* @param $callback
* @return self
*/
public function setValidationResponseCallback($callback) {
$this->validationResponseCallback = $callback;

return $this;
}

/**
* Returns the appropriate response up the controller chain
* if {@link validate()} fails (which is checked prior to executing any form actions).
* By default, returns different views for ajax/non-ajax request, and
* handles 'application/json' requests with a JSON object containing the error messages.
* Behaviour can be influenced by setting {@link $redirectToFormOnValidationError}.
* Behaviour can be influenced by setting {@link $redirectToFormOnValidationError},
* and can be overruled by setting {@link $validationResponseCallback}.
*
* @return SS_HTTPResponse|string
*/
protected function getValidationErrorResponse() {
$callback = $this->getValidationResponseCallback();
if($callback && $callbackResponse = $callback()) {
return $callbackResponse;
}

$request = $this->getRequest();
if($request->isAjax()) {
// Special case for legacy Validator.js implementation
Expand Down

0 comments on commit 746322a

Please sign in to comment.