Skip to content
Browse files

Changing FormHelper::button() to actually create <button> elements in…

…stead of input elements. Test cases added.
  • Loading branch information...
1 parent 08f07a9 commit fa6b1b1a20f8506f17688239c47839815f7f8c3c @markstory markstory committed
Showing with 18 additions and 25 deletions.
  1. +12 −13 cake/libs/view/helpers/form.php
  2. +1 −1 cake/libs/view/helpers/html.php
  3. +5 −11 cake/tests/cases/libs/view/helpers/form.test.php
View
25 cake/libs/view/helpers/form.php
@@ -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
));
}
View
2 cake/libs/view/helpers/html.php
@@ -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>',
View
16 cake/tests/cases/libs/view/helpers/form.test.php
@@ -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);

0 comments on commit fa6b1b1

Please sign in to comment.
Something went wrong with that request. Please try again.