Added buffering of form tags generated by FormHelper::postLink()

This commit is contained in:
ADmad 2013-11-03 22:59:10 +05:30
parent afd182898f
commit d647fe8a91
5 changed files with 91 additions and 6 deletions

View file

@ -7242,6 +7242,74 @@ class FormHelperTest extends CakeTestCase {
$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
*

View file

@ -1736,7 +1736,12 @@ class FormHelper extends AppHelper {
* - `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.
@ -1747,6 +1752,12 @@ 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) {
$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']);
@ -1787,6 +1798,12 @@ class FormHelper extends AppHelper {
$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) {

View file

@ -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; ?>

View file

@ -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>';

View file

@ -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>";
@ -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";