Added support for confirm (message) option to submit in FormHelper

This commit is contained in:
Mark van Driel 2016-08-20 12:51:13 +02:00
parent d66011d0fa
commit 34afc377ec
3 changed files with 39 additions and 6 deletions

View file

@ -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\(\&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'
));
}
@ -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);
}
/**

View file

@ -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);
}

View file

@ -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);