diff --git a/cake/libs/view/helpers/form.php b/cake/libs/view/helpers/form.php index 6c8e72d31..796ffffcb 100644 --- a/cake/libs/view/helpers/form.php +++ b/cake/libs/view/helpers/form.php @@ -1289,6 +1289,51 @@ class FormHelper extends AppHelper { return $out; } +/** + * Creates an HTML link, but access the url using method POST. Requires javascript enabled in browser. + * + * ### Options: + * + * - `data` - Array with key/value to pass in input hidden + * - Other options is the same of HtmlHelper::link() method. + * - The option `onclick` will be replaced. + * + * @param string $title The content to be wrapped by tags. + * @param mixed $url Cake-relative URL or array of URL parameters, or external URL (starts with http://) + * @param array $options Array of HTML attributes. + * @param string $confirmMessage JavaScript confirmation message. + * @return string An `` element. + */ + public function postLink($title, $url = null, $options = array(), $confirmMessage = false) { + if (!empty($options['confirm'])) { + $confirmMessage = $options['confirm']; + unset($options['confirm']); + } + + $formName = uniqid('post_'); + $out = $this->create(false, array('url' => $url, 'name' => $formName, 'id' => $formName)); + if (isset($options['data']) && is_array($options['data'])) { + foreach ($options['data'] as $key => $value) { + $out .= $this->hidden($key, array('value' => $value, 'id' => false)); + } + unset($options['data']); + } + $out .= $this->end(); + + $url = '#'; + $onClick = 'document.' . $formName . '.submit();'; + if ($confirmMessage) { + $confirmMessage = str_replace(array("'", '"'), array("\'", '\"'), $confirmMessage); + $options['onclick'] = "if (confirm('{$confirmMessage}')) { {$onClick} }"; + } else { + $options['onclick'] = $onClick; + } + $options['onclick'] .= ' event.returnValue = false; return false;'; + + $out .= $this->Html->link($title, $url, $options); + return $out; + } + /** * Creates a submit button element. This method will generate `` elements that * can be used to submit, and reset forms by using $options. image submits can be created by supplying an diff --git a/cake/tests/cases/libs/view/helpers/form.test.php b/cake/tests/cases/libs/view/helpers/form.test.php index 743b4de3f..551b1ca84 100644 --- a/cake/tests/cases/libs/view/helpers/form.test.php +++ b/cake/tests/cases/libs/view/helpers/form.test.php @@ -5176,6 +5176,40 @@ class FormHelperTest extends CakeTestCase { $this->assertTrue(strpos($result, '') !== false); } +/** + * testPostLink method + * + * @return void + */ + public function testPostLink() { + $result = $this->Form->postLink('Delete', '/posts/delete/1'); + $this->assertTags($result, array( + 'form' => array('method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8', 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/'), + 'div' => array('style' => 'display:none;'), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), + '/div', + '/form', + 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'), + 'Delete', + '/a' + )); + + $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?'); + $this->assertTags($result, array( + 'form' => array('method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8', 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/'), + 'div' => array('style' => 'display:none;'), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), + '/div', + '/form', + 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\('Confirm\?'\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'), + 'Delete', + '/a' + )); + + $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1))); + $this->assertTrue(strpos($result, '') !== false); + } + /** * testSubmitButton method *