Skip to content

Commit

Permalink
Process actions on Form subclasses
Browse files Browse the repository at this point in the history
Regression introduced through silverstripe#6362.

Quote from the RFC:

```
Thus the order of action precedence becomes

action callback
action on the Form
action on the FormRequestHandler
action on any parent controller (if given)
```
  • Loading branch information
chillu committed May 18, 2017
1 parent 8ed675d commit adbf9d9
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Forms/FormRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ public function httpSubmission($request)
return $this->$funcName($vars, $this->form, $request);
}

// Otherwise, try a handler method on the form itself
if ($this->form->hasMethod($funcName)) {
return $this->form->$funcName($vars, $this->form, $request);
}

// Check for inline actions
$field = $this->checkFieldsForAction($this->form->Fields(), $funcName);
if ($field) {
Expand Down
45 changes: 45 additions & 0 deletions tests/php/Forms/FormRequestHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace SilverStripe\Forms\Tests;

use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\FormRequestHandler;
use SilverStripe\Forms\Tests\FormRequestHandlerTest\TestForm;
use SilverStripe\Forms\Tests\FormRequestHandlerTest\TestFormRequestHandler;

class FormRequestHandlerTest extends SapphireTest
{
public function testCallsActionOnFormHandler()
{
$form = new TestForm(
new Controller(),
'Form',
new FieldList(),
new FieldList(new FormAction('mySubmitOnFormHandler'))
);
$form->disableSecurityToken();
$handler = new TestFormRequestHandler($form);
$request = new HTTPRequest('POST', '/', null, ['action_mySubmitOnFormHandler' => 1]);
$response = $handler->httpSubmission($request);
$this->assertFalse($response->isError());
}

public function testCallsActionOnForm()
{
$form = new TestForm(
new Controller(),
'Form',
new FieldList(),
new FieldList(new FormAction('mySubmitOnForm'))
);
$form->disableSecurityToken();
$handler = new FormRequestHandler($form);
$request = new HTTPRequest('POST', '/', null, ['action_mySubmitOnForm' => 1]);
$response = $handler->httpSubmission($request);
$this->assertFalse($response->isError());
}
}
14 changes: 14 additions & 0 deletions tests/php/Forms/FormRequestHandlerTest/FormRequestHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace SilverStripe\Forms\Tests\FormRequestHandlerTest;

use SilverStripe\Control\HTTPResponse;
use SilverStripe\Forms\FormRequestHandler;

class TestFormRequestHandler extends FormRequestHandler
{
public function mySubmitOnFormHandler()
{
return new HTTPResponse('success', 200);
}
}
14 changes: 14 additions & 0 deletions tests/php/Forms/FormRequestHandlerTest/TestForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace SilverStripe\Forms\Tests\FormRequestHandlerTest;

use SilverStripe\Control\HTTPResponse;
use SilverStripe\Forms\Form;

class TestForm extends Form
{
public function mySubmitOnForm()
{
return new HTTPResponse('success', 200);
}
}

0 comments on commit adbf9d9

Please sign in to comment.