Browse Source

Merge pull request #11483 from cakephp/master-button-confirm

Add confirm message for form buttons.
Mark Story 8 years ago
parent
commit
d7cf3e7a1d
2 changed files with 22 additions and 2 deletions
  1. 10 2
      src/View/Helper/FormHelper.php
  2. 12 0
      tests/TestCase/View/Helper/FormHelperTest.php

+ 10 - 2
src/View/Helper/FormHelper.php

@@ -1730,6 +1730,7 @@ class FormHelper extends Helper
      * ### Options:
      *
      * - `escape` - HTML entity encode the $title of the button. Defaults to false.
+     * - `confirm` - Confirm message to show. Form execution will only continue if confirmed then.
      *
      * @param string $title The button's caption. Not automatically HTML encoded
      * @param array $options Array of options and HTML attributes.
@@ -1738,9 +1739,15 @@ class FormHelper extends Helper
      */
     public function button($title, array $options = [])
     {
-        $options += ['type' => 'submit', 'escape' => false, 'secure' => false];
+        $options += ['type' => 'submit', 'escape' => false, 'secure' => false, 'confirm' => null];
         $options['text'] = $title;
 
+        $confirmMessage = $options['confirm'];
+        unset($options['confirm']);
+        if ($confirmMessage) {
+            $options['onclick'] = $this->_confirm($confirmMessage, 'return true;', 'return false;', $options);
+        }
+
         return $this->widget('button', $options);
     }
 
@@ -1757,6 +1764,7 @@ class FormHelper extends Helper
      *   HTTP/1.1 DELETE (or others) request. Defaults to 'post'.
      * - `form` - Array with any option that FormHelper::create() can take
      * - Other options is the same of button method.
+     * - `confirm` - Confirm message to show. Form execution will only continue if confirmed then.
      *
      * @param string $title The button's caption. Not automatically HTML encoded
      * @param string|array $url URL as string or array
@@ -1804,7 +1812,7 @@ class FormHelper extends Helper
      * - `data` - Array with key/value to pass in input hidden
      * - `method` - Request method to use. Set to 'delete' to simulate
      *   HTTP/1.1 DELETE request. Defaults to 'post'.
-     * - `confirm` - Confirm message to show.
+     * - `confirm` - Confirm message to show. Form execution will only continue if confirmed then.
      * - `block` - Set to true to append form to view block "postLink" or provide
      *   custom block name.
      * - Other options are the same of HtmlHelper::link() method.

+ 12 - 0
tests/TestCase/View/Helper/FormHelperTest.php

@@ -7325,6 +7325,18 @@ class FormHelperTest extends TestCase
     }
 
     /**
+     * Test generation of a form button with confirm message.
+     *
+     * @return void
+     */
+    public function testButtonWithConfirm()
+    {
+        $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
+        $expected = ['button' => ['type' => 'submit', 'onclick' => 'if (confirm("Confirm me!")) { return true; } return false;'], 'Hi', '/button'];
+        $this->assertHtml($expected, $result);
+    }
+
+    /**
      * testPostButton method
      *
      * @return void