Skip to content

Commit

Permalink
Added support for confirm (message) option to submit in FormHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdriel committed Aug 20, 2016
1 parent d66011d commit 34afc37
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
28 changes: 25 additions & 3 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -8095,7 +8095,7 @@ public function testPostLink() {
),
'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
'/form',
'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\("Confirm\?"\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\("Confirm\?"\)\) \{ document\.post_\w+\.submit\(\); \} else \{ \} event\.returnValue = false; return false;/'),
'Delete',
'/a'
));
Expand All @@ -8108,7 +8108,7 @@ public function testPostLink() {
),
'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
'/form',
'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\("'Confirm' this \\\\"deletion\\\\"\?"\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\("'Confirm' this \\\\"deletion\\\\"\?"\)\) \{ document\.post_\w+\.submit\(\); \} else \{ \} event\.returnValue = false; return false;/'),
'Delete',
'/a'
));
Expand Down Expand Up @@ -8142,7 +8142,7 @@ public function testPostLink() {
),
'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
'/form',
'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} else \{ \} event\.returnValue = false; return false;/'),
'/a'
));
}
Expand Down Expand Up @@ -8448,6 +8448,17 @@ public function testSubmitButton() {
'/div'
);
$this->assertTags($result, $expected);

$result = $this->Form->submit('Test', array('confirm' => 'Confirm?'));
$expected = array(
'div' => array('class' => 'submit'),
'input' => array(
'type' => 'submit', 'value' => 'Test',
'onclick' => 'preg:/if \(confirm\("Confirm\?"\)\) \{ \} else \{ event\.returnValue = false; return false; \}/'
),
'/div'
);
$this->assertTags($result, $expected);
}

/**
Expand Down Expand Up @@ -8527,6 +8538,17 @@ public function testSubmitImage() {
'/div'
);
$this->assertTags($result, $expected);

$result = $this->Form->submit('cake.power.gif', array('confirm' => 'Confirm?'));
$expected = array(
'div' => array('class' => 'submit'),
'input' => array(
'type' => 'image', 'src' => 'img/cake.power.gif',
'onclick' => 'preg:/if \(confirm\("Confirm\?"\)\) \{ \} else \{ event\.returnValue = false; return false; \}/'
),
'/div'
);
$this->assertTags($result, $expected);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/View/Helper.php
Expand Up @@ -552,7 +552,7 @@ protected function _formatAttribute($key, $value, $escape = true) {
*/
protected function _confirm($message, $okCode, $cancelCode = '', $options = array()) {
$message = json_encode($message);
$confirm = "if (confirm({$message})) { {$okCode} } {$cancelCode}";
$confirm = "if (confirm({$message})) { {$okCode} } else { {$cancelCode} }";
if (isset($options['escape']) && $options['escape'] === false) {
$confirm = h($confirm);
}
Expand Down
15 changes: 13 additions & 2 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -1920,9 +1920,9 @@ public function postLink($title, $url = null, $options = array(), $confirmMessag
if ($confirmMessage) {
$options['onclick'] = $this->_confirm($confirmMessage, $onClick, '', $options);
} else {
$options['onclick'] = $onClick . ' ';
$options['onclick'] = $onClick;
}
$options['onclick'] .= 'event.returnValue = false; return false;';
$options['onclick'] .= ' event.returnValue = false; return false;';

$out .= $this->Html->link($title, $url, $options);
return $out;
Expand All @@ -1940,6 +1940,7 @@ public function postLink($title, $url = null, $options = array(), $confirmMessag
* - `before` - Content to include before the input.
* - `after` - Content to include after the input.
* - `type` - Set to 'reset' for reset inputs. Defaults to 'submit'
* - `confirm` - JavaScript confirmation message.
* - Other attributes will be assigned to the input element.
*
* ### Options
Expand All @@ -1957,12 +1958,17 @@ public function postLink($title, $url = null, $options = array(), $confirmMessag
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::submit
*/
public function submit($caption = null, $options = array()) {
$confirmMessage = false;
if (!is_string($caption) && empty($caption)) {
$caption = __d('cake', 'Submit');
}
$out = null;
$div = true;

if (!empty($options['confirm'])) {
$confirmMessage = $options['confirm'];
unset($options['confirm']);
}
if (isset($options['div'])) {
$div = $options['div'];
unset($options['div']);
Expand Down Expand Up @@ -2005,6 +2011,11 @@ public function submit($caption = null, $options = array()) {
}
}

if ($confirmMessage) {
$cancelCode = 'event.returnValue = false; return false;';
$options['onclick'] = $this->_confirm($confirmMessage, '', $cancelCode, $options);
}

if ($isUrl) {
unset($options['type']);
$tag = $this->Html->useTag('submitimage', $caption, $options);
Expand Down

0 comments on commit 34afc37

Please sign in to comment.