diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php index 6ca879919..fab4f12ef 100644 --- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php @@ -8095,7 +8095,7 @@ class FormHelperTest extends CakeTestCase { ), '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 @@ class FormHelperTest extends CakeTestCase { ), '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 @@ class FormHelperTest extends CakeTestCase { ), '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 @@ class FormHelperTest extends CakeTestCase { '/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 @@ class FormHelperTest extends CakeTestCase { '/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 35aefff44..aadbc6c15 100644 --- a/lib/Cake/View/Helper.php +++ b/lib/Cake/View/Helper.php @@ -552,7 +552,7 @@ class Helper extends Object { */ 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 ea190ca6b..2199450c0 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -1920,9 +1920,9 @@ class FormHelper extends AppHelper { 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 @@ class FormHelper extends AppHelper { * - `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 @@ class FormHelper extends AppHelper { * @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 @@ class FormHelper extends AppHelper { } } + 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);