Merge pull request #9314 from mvdriel/add-support-for-confirm-option-to-submit-in-formhelper

Added support for confirm (message) option to submit in FormHelper
This commit is contained in:
Mark Story 2016-08-22 09:18:30 -04:00 committed by GitHub
commit 0565081db6
3 changed files with 35 additions and 1 deletions

View file

@ -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\?"\)\) \{ return true; \} 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\?"\)\) \{ return true; \} event\.returnValue = false; return false;/'
),
'/div'
);
$this->assertTags($result, $expected);
}
/**

View file

@ -546,7 +546,7 @@ class Helper extends Object {
*
* @param string $message Message to be displayed
* @param string $okCode Code to be executed after user chose 'OK'
* @param string $cancelCode Code to be executed after user chose 'Cancel'
* @param string $cancelCode Code to be executed after user chose 'Cancel', also executed when okCode doesn't return
* @param array $options Array of options
* @return string onclick JS code
*/

View file

@ -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,12 @@ class FormHelper extends AppHelper {
}
}
if ($confirmMessage) {
$okCode = 'return true;';
$cancelCode = 'event.returnValue = false; return false;';
$options['onclick'] = $this->_confirm($confirmMessage, $okCode, $cancelCode, $options);
}
if ($isUrl) {
unset($options['type']);
$tag = $this->Html->useTag('submitimage', $caption, $options);