From adbf9d9f718bc7e16e2989674da897f23148a1b4 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Thu, 18 May 2017 22:47:39 +1200 Subject: [PATCH] Process actions on Form subclasses Regression introduced through https://github.com/silverstripe/silverstripe-framework/issues/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) ``` --- src/Forms/FormRequestHandler.php | 5 +++ tests/php/Forms/FormRequestHandlerTest.php | 45 +++++++++++++++++++ .../FormRequestHandler.php | 14 ++++++ .../Forms/FormRequestHandlerTest/TestForm.php | 14 ++++++ 4 files changed, 78 insertions(+) create mode 100644 tests/php/Forms/FormRequestHandlerTest.php create mode 100644 tests/php/Forms/FormRequestHandlerTest/FormRequestHandler.php create mode 100644 tests/php/Forms/FormRequestHandlerTest/TestForm.php diff --git a/src/Forms/FormRequestHandler.php b/src/Forms/FormRequestHandler.php index 67e0d755b2f..bc2ca143786 100644 --- a/src/Forms/FormRequestHandler.php +++ b/src/Forms/FormRequestHandler.php @@ -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) { diff --git a/tests/php/Forms/FormRequestHandlerTest.php b/tests/php/Forms/FormRequestHandlerTest.php new file mode 100644 index 00000000000..2206c856d4c --- /dev/null +++ b/tests/php/Forms/FormRequestHandlerTest.php @@ -0,0 +1,45 @@ +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()); + } +} diff --git a/tests/php/Forms/FormRequestHandlerTest/FormRequestHandler.php b/tests/php/Forms/FormRequestHandlerTest/FormRequestHandler.php new file mode 100644 index 00000000000..6de7ffe2b65 --- /dev/null +++ b/tests/php/Forms/FormRequestHandlerTest/FormRequestHandler.php @@ -0,0 +1,14 @@ +