Skip to content

Commit

Permalink
Merge pull request #11483 from cakephp/master-button-confirm
Browse files Browse the repository at this point in the history
Add confirm message for form buttons.
  • Loading branch information
markstory committed Dec 1, 2017
2 parents fbd230b + 9b72115 commit d7cf3e7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/View/Helper/FormHelper.php
Expand Up @@ -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.
Expand All @@ -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);
}

Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -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
*
Expand Down

0 comments on commit d7cf3e7

Please sign in to comment.