Skip to content

Commit

Permalink
Implement a Helper method to generate confirm() links
Browse files Browse the repository at this point in the history
This allows for overriding the default behavior of showing
confirm()-dialogs in Html and Form helpers.
  • Loading branch information
Alexander Hofbauer committed Aug 6, 2013
1 parent a54c92f commit 8601e00
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -7018,7 +7018,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\(\); \} event\.returnValue = false; return false;/'),
'Delete',
'/a'
));
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php
Expand Up @@ -215,7 +215,7 @@ public function testLink() {

$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'
);
Expand Down
13 changes: 13 additions & 0 deletions lib/Cake/View/Helper.php
Expand Up @@ -496,6 +496,19 @@ protected function _formatAttribute($key, $value, $escape = true) {
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.
*
Expand Down
7 changes: 3 additions & 4 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -1784,12 +1784,11 @@ public function postLink($title, $url = null, $options = array(), $confirmMessag
$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;
Expand Down
9 changes: 4 additions & 5 deletions lib/Cake/View/Helper/HtmlHelper.php
Expand Up @@ -359,15 +359,14 @@ public function link($title, $url = null, $options = array(), $confirmMessage =
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);
Expand Down

0 comments on commit 8601e00

Please sign in to comment.