Skip to content

Commit

Permalink
Merge pull request #811 from brq/2.3
Browse files Browse the repository at this point in the history
Allow postLink() to support other HTTP methods; it defaults to POST
  • Loading branch information
markstory committed Sep 2, 2012
2 parents 21a51a3 + fca98e3 commit a1edede
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
13 changes: 13 additions & 0 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -6125,6 +6125,19 @@ public function testPostLink() {
'/a'
));

$result = $this->Form->postLink('Delete', '/posts/delete/1', array('method'=>'delete'));
$this->assertTags($result, array(
'form' => array(
'method' => 'post', 'action' => '/posts/delete/1',
'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
),
'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
'/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(
Expand Down
9 changes: 7 additions & 2 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -1578,7 +1578,7 @@ public function postButton($title, $url, $options = array()) {
}

/**
* Creates an HTML link, but access the url using method POST.
* Creates an HTML link, but access the url using the method you specify (defaults to POST).
* Requires javascript to be enabled in browser.
*
* This method creates a `<form>` element. So do not use this method inside an existing form.
Expand All @@ -1599,6 +1599,11 @@ public function postButton($title, $url, $options = array()) {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::postLink
*/
public function postLink($title, $url = null, $options = array(), $confirmMessage = false) {
$requestMethod = 'POST';
if (!empty($options['method'])) {
$requestMethod = strtoupper($options['method']);
unset($options['method']);
}
if (!empty($options['confirm'])) {
$confirmMessage = $options['confirm'];
unset($options['confirm']);
Expand All @@ -1607,7 +1612,7 @@ public function postLink($title, $url = null, $options = array(), $confirmMessag
$formName = uniqid('post_');
$formUrl = $this->url($url);
$out = $this->Html->useTag('form', $formUrl, array('name' => $formName, 'id' => $formName, 'style' => 'display:none;', 'method' => 'post'));
$out .= $this->Html->useTag('hidden', '_method', ' value="POST"');
$out .= $this->Html->useTag('hidden', '_method', ' value="' . $requestMethod . '"');
$out .= $this->_csrfField();

$fields = array();
Expand Down

0 comments on commit a1edede

Please sign in to comment.