Created the method FormHelper::postButton() to create a button with form to send data via POST.

This commit is contained in:
Juan Basso 2010-10-18 17:53:27 -02:00
parent d771239104
commit 4c106490ef
2 changed files with 48 additions and 0 deletions

View file

@ -1263,6 +1263,32 @@ class FormHelper extends AppHelper {
);
}
/**
* Create a `<button>` tag with `<form>` using POST method.
*
* ### Options:
*
* - `data` - Array with key/value to pass in input hidden
* - Other options is the same of button method.
*
* @param string $title The button's caption. Not automatically HTML encoded
* @param mixed $url URL as string or array
* @param array $options Array of options and HTML attributes.
* @return string A HTML button tag.
*/
public function postButton($title, $url, $options = array()) {
$out = $this->create(false, array('id' => false, 'url' => $url));
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->button($title, $options);
$out .= $this->end();
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

View file

@ -5154,6 +5154,28 @@ class FormHelperTest extends CakeTestCase {
$this->assertNoPattern('/\&039/', $result);
}
/**
* testPostButton method
*
* @return void
*/
public function testPostButton() {
$result = $this->Form->postButton('Hi', '/controller/action');
$this->assertTags($result, array(
'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
'div' => array('style' => 'display:none;'),
'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
'/div',
'button' => array('type' => 'submit'),
'Hi',
'/button',
'/form'
));
$result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
$this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value" />') !== false);
}
/**
* testSubmitButton method
*