Implement a Helper method to generate confirm() links

This allows for overriding the default behavior of showing
confirm()-dialogs in Html and Form helpers.
This commit is contained in:
Alexander Hofbauer 2013-08-05 16:20:53 +02:00
parent a54c92fc0f
commit 8601e0078a
5 changed files with 22 additions and 11 deletions

View file

@ -7018,7 +7018,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\(\); \} event\.returnValue = false; return false;/'),
'Delete',
'/a'
));

View file

@ -215,7 +215,7 @@ class HtmlHelperTest extends CakeTestCase {
$result = $this->Html->link('Home', '/home', array('confirm' => 'Are you sure you want to do this?'));
$expected = array(
'a' => array('href' => '/home', 'onclick' => 'return confirm('Are you sure you want to do this?');'),
'a' => array('href' => '/home', 'onclick' => 'if (confirm("Are you sure you want to do this?")) { return true; } return false;'),
'Home',
'/a'
);

View file

@ -496,6 +496,19 @@ class Helper extends Object {
return sprintf($this->_attributeFormat, $key, ($escape ? h($value) : $value));
}
/**
* Returns a string to be used as onclick handler for confirm dialogs.
*
* @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'
* @return string onclick JS code
*/
protected function _confirm($message, $okCode, $cancelCode = '') {
$message = json_encode($message);
return "if (confirm({$message})) { {$okCode} } {$cancelCode}";
}
/**
* Sets this helper's model and field properties to the dot-separated value-pair in $entity.
*

View file

@ -1784,12 +1784,11 @@ class FormHelper extends AppHelper {
$url = '#';
$onClick = 'document.' . $formName . '.submit();';
if ($confirmMessage) {
$confirmMessage = str_replace(array("'", '"'), array("\'", '\"'), $confirmMessage);
$options['onclick'] = "if (confirm('{$confirmMessage}')) { {$onClick} }";
$options['onclick'] = $this->_confirm($confirmMessage, $onClick);
} 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;

View file

@ -359,15 +359,14 @@ class HtmlHelper extends AppHelper {
unset($options['confirm']);
}
if ($confirmMessage) {
$confirmMessage = str_replace("'", "\'", $confirmMessage);
$confirmMessage = str_replace('"', '\"', $confirmMessage);
$options['onclick'] = "return confirm('{$confirmMessage}');";
$options['onclick'] = $this->_confirm($confirmMessage, 'return true;', 'return false;');
} elseif (isset($options['default']) && !$options['default']) {
if (isset($options['onclick'])) {
$options['onclick'] .= ' event.returnValue = false; return false;';
$options['onclick'] .= ' ';
} else {
$options['onclick'] = 'event.returnValue = false; return false;';
$options['onclick'] = '';
}
$options['onclick'] .= 'event.returnValue = false; return false;';
unset($options['default']);
}
return sprintf($this->_tags['link'], $url, $this->_parseAttributes($options), $title);