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

This commit is contained in:
brq 2012-09-02 12:50:08 -05:00
parent 21a51a374a
commit fca98e39f9
2 changed files with 20 additions and 2 deletions

View file

@ -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(

View file

@ -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 `<form>` 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();