Skip to content
Permalink
Browse files

Allow postLink() to support other HTTP methods; it defaults to POST

  • Loading branch information...
brq committed Sep 2, 2012
1 parent 21a51a3 commit fca98e39f9041c1aee915968e0523dfc83e0c2c8
Showing with 20 additions and 2 deletions.
  1. +13 −0 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
  2. +7 −2 lib/Cake/View/Helper/FormHelper.php
@@ -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(
@@ -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.
@@ -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']);
@@ -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();

0 comments on commit fca98e3

Please sign in to comment.
You can’t perform that action at this time.