Browse Source

Pass exception instance to validation failure callback.

ADmad 6 years ago
parent
commit
4a33563590

+ 10 - 9
src/Controller/Component/FormProtectionComponent.php

@@ -137,27 +137,28 @@ class FormProtectionComponent extends Component
      */
     protected function validationFailure(FormProtector $formProtector): ?Response
     {
-        if ($this->_config['validationFailureCallback']) {
-            return $this->executeCallback($this->_config['validationFailureCallback'], $formProtector);
+        if (Configure::read('debug')) {
+            $exception = new BadRequestException($formProtector->getError());
+        } else {
+            $exception = new BadRequestException(static::DEFAULT_EXCEPTION_MESSAGE);
         }
 
-        if (!Configure::read('debug')) {
-            throw new BadRequestException(static::DEFAULT_EXCEPTION_MESSAGE);
+        if ($this->_config['validationFailureCallback']) {
+            return $this->executeCallback($this->_config['validationFailureCallback'], $exception);
         }
 
-        $errorMessage = $formProtector->getError();
-        throw new BadRequestException($errorMessage);
+        throw $exception;
     }
 
     /**
      * Execute callback.
      *
      * @param \Closure $callback A valid callable
-     * @param @param \Cake\Form\FormProtector $formProtector Form Protector instance.
+     * @param \Cake\Http\Exception\BadRequestException $exception Exception instance.
      * @return \Cake\Http\Response|null
      */
-    protected function executeCallback(Closure $callback, FormProtector $formProtector): ?Response
+    protected function executeCallback(Closure $callback, BadRequestException $exception): ?Response
     {
-        return $callback($formProtector);
+        return $callback($exception);
     }
 }

+ 2 - 3
tests/TestCase/Controller/Component/FormProtectionComponentTest.php

@@ -19,7 +19,6 @@ namespace Cake\Test\TestCase\Controller\Component;
 use Cake\Controller\Component\FormProtectionComponent;
 use Cake\Controller\Controller;
 use Cake\Event\Event;
-use Cake\Form\FormProtector;
 use Cake\Http\Exception\BadRequestException;
 use Cake\Http\Exception\NotFoundException;
 use Cake\Http\Response;
@@ -181,7 +180,7 @@ class FormProtectionComponentTest extends TestCase
 
     public function testCallbackReturnResponse()
     {
-        $this->FormProtection->setConfig('validationFailureCallback', function (FormProtector $formProtector) {
+        $this->FormProtection->setConfig('validationFailureCallback', function (BadRequestException $exception) {
             return new Response(['body' => 'from callback']);
         });
 
@@ -213,7 +212,7 @@ class FormProtectionComponentTest extends TestCase
         $this->expectException(NotFoundException::class);
         $this->expectExceptionMessage('error description');
 
-        $this->FormProtection->setConfig('validationFailureCallback', function (FormProtector $formProtector) {
+        $this->FormProtection->setConfig('validationFailureCallback', function (BadRequestException $exception) {
             throw new NotFoundException('error description');
         });