From fca98e39f9041c1aee915968e0523dfc83e0c2c8 Mon Sep 17 00:00:00 2001 From: brq Date: Sun, 2 Sep 2012 12:50:08 -0500 Subject: [PATCH] Allow postLink() to support other HTTP methods; it defaults to POST --- lib/Cake/Test/Case/View/Helper/FormHelperTest.php | 13 +++++++++++++ lib/Cake/View/Helper/FormHelper.php | 9 +++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php index fcaf57d1e..75dff5068 100644 --- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php @@ -6125,6 +6125,19 @@ class FormHelperTest extends CakeTestCase { '/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( diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index d78531fdf..de3824529 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -1578,7 +1578,7 @@ class FormHelper extends AppHelper { } /** - * 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 `
` element. So do not use this method inside an existing form. @@ -1599,6 +1599,11 @@ class FormHelper extends AppHelper { * @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 @@ class FormHelper extends AppHelper { $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();