mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-09-04 10:32:40 +00:00
Created the method FormHelper::postLink() to create a link with form to send data via POST. This feature require javascript.
This commit is contained in:
parent
4c106490ef
commit
373fa780f7
2 changed files with 79 additions and 0 deletions
|
@ -1289,6 +1289,51 @@ class FormHelper extends AppHelper {
|
|||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an HTML link, but access the url using method POST. Requires javascript enabled in browser.
|
||||
*
|
||||
* ### Options:
|
||||
*
|
||||
* - `data` - Array with key/value to pass in input hidden
|
||||
* - Other options is the same of HtmlHelper::link() method.
|
||||
* - The option `onclick` will be replaced.
|
||||
*
|
||||
* @param string $title The content to be wrapped by <a> tags.
|
||||
* @param mixed $url Cake-relative URL or array of URL parameters, or external URL (starts with http://)
|
||||
* @param array $options Array of HTML attributes.
|
||||
* @param string $confirmMessage JavaScript confirmation message.
|
||||
* @return string An `<a />` element.
|
||||
*/
|
||||
public function postLink($title, $url = null, $options = array(), $confirmMessage = false) {
|
||||
if (!empty($options['confirm'])) {
|
||||
$confirmMessage = $options['confirm'];
|
||||
unset($options['confirm']);
|
||||
}
|
||||
|
||||
$formName = uniqid('post_');
|
||||
$out = $this->create(false, array('url' => $url, 'name' => $formName, 'id' => $formName));
|
||||
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();
|
||||
|
||||
$url = '#';
|
||||
$onClick = 'document.' . $formName . '.submit();';
|
||||
if ($confirmMessage) {
|
||||
$confirmMessage = str_replace(array("'", '"'), array("\'", '\"'), $confirmMessage);
|
||||
$options['onclick'] = "if (confirm('{$confirmMessage}')) { {$onClick} }";
|
||||
} else {
|
||||
$options['onclick'] = $onClick;
|
||||
}
|
||||
$options['onclick'] .= ' event.returnValue = false; return false;';
|
||||
|
||||
$out .= $this->Html->link($title, $url, $options);
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a submit button element. This method will generate `<input />` elements that
|
||||
* can be used to submit, and reset forms by using $options. image submits can be created by supplying an
|
||||
|
|
|
@ -5176,6 +5176,40 @@ class FormHelperTest extends CakeTestCase {
|
|||
$this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value" />') !== false);
|
||||
}
|
||||
|
||||
/**
|
||||
* testPostLink method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
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', 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/'),
|
||||
'div' => array('style' => 'display:none;'),
|
||||
'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
|
||||
'/div',
|
||||
'/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('method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8', 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/'),
|
||||
'div' => array('style' => 'display:none;'),
|
||||
'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
|
||||
'/div',
|
||||
'/form',
|
||||
'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\('Confirm\?'\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
|
||||
'Delete',
|
||||
'/a'
|
||||
));
|
||||
|
||||
$result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
|
||||
$this->assertTrue(strpos($result, '<input type="hidden" name="data[id]" value="1" />') !== false);
|
||||
}
|
||||
|
||||
/**
|
||||
* testSubmitButton method
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue