Skip to content

Commit

Permalink
Changing FormHelper::button() to actually create <button> elements in…
Browse files Browse the repository at this point in the history
…stead of input elements. Test cases added.
  • Loading branch information
markstory committed Oct 20, 2009
1 parent 08f07a9 commit fa6b1b1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 25 deletions.
25 changes: 12 additions & 13 deletions cake/libs/view/helpers/form.php
Expand Up @@ -1107,28 +1107,27 @@ function file($fieldName, $options = array()) {
}

/**
* Creates a button tag.
* Creates a <button> tag.
*
* @param string $title The button's caption
* @param array $options Array of options.
* Options:
*
* - `escape` - HTML entity encode the $title of the button. Defaults to false.
*
* @param string $title The button's caption. Not automatically HTML encoded
* @param array $options Array of options and HTML attributes.
* @return string A HTML button tag.
* @access public
*/
function button($title, $options = array()) {
$options = array_merge(array('type' => 'button', 'value' => $title), $options);

if (isset($options['name']) && strpos($options['name'], '.') !== false) {
if ($this->value($options['name'])) {
$options['checked'] = 'checked';
}
$name = $options['name'];
unset($options['name']);
$options = $this->_initInputField($name, $options);
$options += array('type' => 'submit', 'escape' => false);
if ($options['escape']) {
$title = h($title);
}
return $this->output(sprintf(
$this->Html->tags['button'],
$options['type'],
$this->_parseAttributes($options, array('type'), '', ' ')
$this->_parseAttributes($options, array('type'), '', ' '),
$title
));
}

Expand Down
2 changes: 1 addition & 1 deletion cake/libs/view/helpers/html.php
Expand Up @@ -60,7 +60,7 @@ class HtmlHelper extends AppHelper {
'file_no_model' => '<input type="file" name="%s" %s/>',
'submit' => '<input %s/>',
'submitimage' => '<input type="image" src="%s" %s/>',
'button' => '<input type="%s" %s/>',
'button' => '<button type="%s"%s>%s</button>',
'image' => '<img src="%s" %s/>',
'tableheader' => '<th%s>%s</th>',
'tableheaderrow' => '<tr%s>%s</tr>',
Expand Down
16 changes: 5 additions & 11 deletions cake/tests/cases/libs/view/helpers/form.test.php
Expand Up @@ -4491,19 +4491,13 @@ function testFileUploadOnOtherModel() {
*/
function testButton() {
$result = $this->Form->button('Hi');
$this->assertTags($result, array('input' => array('type' => 'button', 'value' => 'Hi')));
$this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));

$result = $this->Form->button('Clear Form', array('type' => 'clear'));
$this->assertTags($result, array('input' => array('type' => 'clear', 'value' => 'Clear Form')));
$result = $this->Form->button('Clear Form >', array('type' => 'reset'));
$this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));

$result = $this->Form->button('Reset Form', array('type' => 'reset'));
$this->assertTags($result, array('input' => array('type' => 'reset', 'value' => 'Reset Form')));

$result = $this->Form->button('Options', array('type' => 'reset', 'name' => 'Post.options'));
$this->assertTags($result, array('input' => array('type' => 'reset', 'name' => 'data[Post][options]', 'id' => 'PostOptions', 'value' => 'Options')));

$result = $this->Form->button('Options', array('type' => 'reset', 'name' => 'Post.options', 'id' => 'Opt'));
$this->assertTags($result, array('input' => array('type' => 'reset', 'name' => 'data[Post][options]', 'id' => 'Opt', 'value' => 'Options')));
$result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
$this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));

$result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
$this->assertNoPattern('/\&039/', $result);
Expand Down

0 comments on commit fa6b1b1

Please sign in to comment.