From 34afc377ec0b84e7c684854a8cd2428084af9abf Mon Sep 17 00:00:00 2001 From: Mark van Driel Date: Sat, 20 Aug 2016 12:51:13 +0200 Subject: [PATCH] Added support for confirm (message) option to submit in FormHelper --- .../Test/Case/View/Helper/FormHelperTest.php | 28 +++++++++++++++++-- lib/Cake/View/Helper.php | 2 +- lib/Cake/View/Helper/FormHelper.php | 15 ++++++++-- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php index 6ca879919db..fab4f12ef45 100644 --- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php @@ -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' )); @@ -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' )); @@ -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\(\"\;Confirm thing\"\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'), + 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\"\;Confirm thing\"\;\)\) \{ document\.post_\w+\.submit\(\); \} else \{ \} event\.returnValue = false; return false;/'), '/a' )); } @@ -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); } /** @@ -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); } /** diff --git a/lib/Cake/View/Helper.php b/lib/Cake/View/Helper.php index 35aefff4445..aadbc6c152e 100644 --- a/lib/Cake/View/Helper.php +++ b/lib/Cake/View/Helper.php @@ -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); } diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index ea190ca6bf9..2199450c089 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -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; @@ -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 @@ -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']); @@ -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);