Skip to content

Commit

Permalink
Added buffering of form tags generated by FormHelper::postLink()
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Nov 3, 2013
1 parent afd1828 commit d647fe8
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 6 deletions.
68 changes: 68 additions & 0 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -7242,6 +7242,74 @@ public function testSecurePostLink() {
$this->assertTags($result, $expected);
}

/**
* Test that postLink adds form tags to view block
*
* @return void
*/
public function testPostLinkFormBuffer() {
$result = $this->Form->postLink('Delete', '/posts/delete/1', array('inline' => false));
$this->assertTags($result, array(
'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
'Delete',
'/a'
));

$result = $this->View->fetch('postLink');
$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' => 'POST'),
'/form'
));

$result = $this->Form->postLink('Delete', '/posts/delete/2',
array('inline' => false, 'method' => 'DELETE')
);
$this->assertTags($result, array(
'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
'Delete',
'/a'
));

$result = $this->View->fetch('postLink');
$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' => 'POST'),
'/form',
array(
'form' => array(
'method' => 'post', 'action' => '/posts/delete/2',
'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
),
),
array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
'/form'
));

$result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
$this->assertTags($result, array(
'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
'Delete',
'/a'
));

$result = $this->View->fetch('foobar');
$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' => 'POST'),
'/form'
));
}

/**
* testSubmitButton method
*
Expand Down
19 changes: 18 additions & 1 deletion lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -1736,7 +1736,12 @@ public function postButton($title, $url, $options = array()) {
* - `data` - Array with key/value to pass in input hidden
* - `method` - Request method to use. Set to 'delete' to simulate HTTP/1.1 DELETE request. Defaults to 'post'.
* - `confirm` - Can be used instead of $confirmMessage.
* - Other options is the same of HtmlHelper::link() method.
* - `inline` - Whether or not the associated form tag should be output inline.
* Set to false to have the form tag appended to the 'postLink' view block.
* Defaults to true.
* - `block` - Choose a custom block to append the form tag to. Using this option
* will override the inline option.
* - Other options are the same of HtmlHelper::link() method.
* - The option `onclick` will be replaced.
*
* @param string $title The content to be wrapped by <a> tags.
Expand All @@ -1747,6 +1752,12 @@ 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) {
$options += array('inline' => true, 'block' => null);
if (!$options['inline'] && empty($options['block'])) {
$options['block'] = __FUNCTION__;
}
unset($options['inline']);

$requestMethod = 'POST';
if (!empty($options['method'])) {
$requestMethod = strtoupper($options['method']);
Expand Down Expand Up @@ -1787,6 +1798,12 @@ public function postLink($title, $url = null, $options = array(), $confirmMessag
$out .= $this->secure($fields);
$out .= $this->Html->useTag('formend');

if ($options['block']) {
$this->_View->append($options['block'], $out);
$out = '';
}
unset($options['block']);

$url = '#';
$onClick = 'document.' . $formName . '.submit();';
if ($confirmMessage) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/View/Scaffolds/form.ctp
Expand Up @@ -31,7 +31,7 @@
<li><?php echo $this->Form->postLink(
__d('cake', 'Delete'),
array('action' => 'delete', $this->Form->value($modelClass . '.' . $primaryKey)),
null,
array(),
__d('cake', 'Are you sure you want to delete # %s?', $this->Form->value($modelClass . '.' . $primaryKey)));
?></li>
<?php endif; ?>
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/View/Scaffolds/index.ctp
Expand Up @@ -51,7 +51,7 @@ foreach (${$pluralVar} as ${$singularVar}):
echo ' ' . $this->Form->postLink(
__d('cake', 'Delete'),
array('action' => 'delete', ${$singularVar}[$modelClass][$primaryKey]),
null,
array(),
__d('cake', 'Are you sure you want to delete # %s?', ${$singularVar}[$modelClass][$primaryKey])
);
echo '</td>';
Expand Down
6 changes: 3 additions & 3 deletions lib/Cake/View/Scaffolds/view.ctp
Expand Up @@ -51,11 +51,11 @@ foreach ($scaffoldFields as $_field) {
<ul>
<?php
echo "\t\t<li>";
echo $this->Html->link(__d('cake', 'Edit %s', $singularHumanName), array('action' => 'edit', ${$singularVar}[$modelClass][$primaryKey]));
echo $this->Html->link(__d('cake', 'Edit %s', $singularHumanName), array('action' => 'edit', ${$singularVar}[$modelClass][$primaryKey]));
echo " </li>\n";

echo "\t\t<li>";
echo $this->Form->postLink(__d('cake', 'Delete %s', $singularHumanName), array('action' => 'delete', ${$singularVar}[$modelClass][$primaryKey]), null, __d('cake', 'Are you sure you want to delete # %s?', ${$singularVar}[$modelClass][$primaryKey]));
echo $this->Form->postLink(__d('cake', 'Delete %s', $singularHumanName), array('action' => 'delete', ${$singularVar}[$modelClass][$primaryKey]), array(), __d('cake', 'Are you sure you want to delete # %s?', ${$singularVar}[$modelClass][$primaryKey]));
echo " </li>\n";

echo "\t\t<li>";
Expand Down Expand Up @@ -175,7 +175,7 @@ $otherSingularVar = Inflector::variable($_alias);
echo $this->Form->postLink(
__d('cake', 'Delete'),
array('plugin' => $_details['plugin'], 'controller' => $_details['controller'], 'action' => 'delete', ${$otherSingularVar}[$_details['primaryKey']]),
null,
array(),
__d('cake', 'Are you sure you want to delete # %s?', ${$otherSingularVar}[$_details['primaryKey']])
);
echo "\n";
Expand Down

0 comments on commit d647fe8

Please sign in to comment.