Skip to content
Permalink
Browse files

Add confirm message for form buttons.

  • Loading branch information...
dereuromark committed Nov 30, 2017
1 parent fbd230b commit 9b7211557a0c50899166984dbd60e56817ee5971
Showing with 22 additions and 2 deletions.
  1. +10 −2 src/View/Helper/FormHelper.php
  2. +12 −0 tests/TestCase/View/Helper/FormHelperTest.php
@@ -1730,6 +1730,7 @@ public function file($fieldName, array $options = [])
* ### 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 @@ public function file($fieldName, array $options = [])
*/
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 @@ public function button($title, array $options = [])
* 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 @@ public function postButton($title, $url, array $options = [])
* - `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.
@@ -7324,6 +7324,18 @@ public function testButtonUnlockedByDefault()
$this->assertEquals(['save'], $result);
}
/**
* 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
*

0 comments on commit 9b72115

Please sign in to comment.
You can’t perform that action at this time.