Skip to content

Commit

Permalink
Optimization in FormHelper::postLink().
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Jul 19, 2011
1 parent 222df2c commit a2e7c0f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -5666,7 +5666,7 @@ public function testPostLink() {
$result = $this->Form->postLink('Delete', '/posts/delete/1');
$this->assertTags($result, array(
'form' => array(
'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
'method' => 'post', 'action' => '/posts/delete/1',
'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
),
'div' => array('style' => 'display:none;'),
Expand All @@ -5681,7 +5681,7 @@ public function testPostLink() {
$result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
$this->assertTags($result, array(
'form' => array(
'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
'method' => 'post', 'action' => '/posts/delete/1',
'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
),
'div' => array('style' => 'display:none;'),
Expand Down
13 changes: 8 additions & 5 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -335,8 +335,8 @@ public function create($model = null, $options = array()) {
if ($model !== false) {
$object = $this->_getModel($model);
$key = $this->_introspectModel($model, 'key');
$this->setEntity($model, true);
}
$this->setEntity($model, true);

if ($model !== false && $key) {
$recordExists = (
Expand Down Expand Up @@ -419,7 +419,6 @@ public function create($model = null, $options = array()) {
}
$this->requestType = strtolower($options['type']);


$action = $this->url($options['action']);
unset($options['type'], $options['action']);

Expand Down Expand Up @@ -457,7 +456,9 @@ public function create($model = null, $options = array()) {
$append = $this->Html->useTag('block', ' style="display:none;"', $append);
}

$this->setEntity($model, true);
if ($model !== false) {
$this->setEntity($model, true);
}
return $this->Html->useTag('form', $action, $htmlAttributes) . $append;
}

Expand Down Expand Up @@ -1504,14 +1505,16 @@ public function postLink($title, $url = null, $options = array(), $confirmMessag
}

$formName = uniqid('post_');
$out = $this->create(false, array('url' => $url, 'name' => $formName, 'id' => $formName, 'style' => 'display:none;'));
$formUrl = $this->url($url);
$out = $this->Html->useTag('form', $formUrl, array('name' => $formName, 'id' => $formName, 'style' => 'display:none;', 'method' => 'post'));
$out .= $this->Html->useTag('block', ' style="display:none;"', $this->Html->useTag('hidden', '_method', ' value="POST"'));
if (isset($options['data']) && is_array($options['data'])) {
foreach ($options['data'] as $key => $value) {
$out .= $this->hidden($key, array('value' => $value, 'id' => false));
}
unset($options['data']);
}
$out .= $this->end();
$out .= $this->Html->useTag('formend');

$url = '#';
$onClick = 'document.' . $formName . '.submit();';
Expand Down

0 comments on commit a2e7c0f

Please sign in to comment.