Implementing FormHelper::button(), with options for alternate button types (Ticket #2341)

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6188 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2007-12-22 01:27:50 +00:00
parent e6af521843
commit 5e7f3a8287
3 changed files with 35 additions and 20 deletions

View file

@ -891,33 +891,23 @@ class FormHelper extends AppHelper {
/** /**
* Creates a button tag. * Creates a button tag.
* *
* @param mixed $params Array of params [content, type, options] or the * @param string $title The button's caption
* content of the button.
* @param string $type Type of the button (button, submit or reset).
* @param array $options Array of options. * @param array $options Array of options.
* @return string A HTML button tag. * @return string A HTML button tag.
* @access public * @access public
*/ */
function button($params, $type = 'button', $options = array()) { function button($title, $options = array()) {
$options = array_merge(array('type' => 'button', 'value' => $title), $options);
trigger_error(__("Don't use me yet"), E_USER_ERROR); if (isset($options['name']) && (strpos($options['name'], "/") !== false || strpos($options['name'], ".") !== false)) {
if (isset($options['name'])) { if ($this->value($options['name'])) {
if (strpos($options['name'], "/") !== false || strpos($options['name'], ".") !== false) { $options['checked'] = 'checked';
if ($this->value($options['name'])) {
$options['checked'] = 'checked';
}
$this->setFieldName($options['name']);
$options['name'] = 'data[' . $this->model() . '][' . $this->field() . ']';
} }
$name = $options['name'];
unset($options['name']);
$options = $this->__initInputField($name, $options);
} }
return $this->output(sprintf($this->Html->tags['button'], $options['type'], $this->_parseAttributes($options, array('type'), '', ' ')));
$options['type'] = $type;
$values = array(
'options' => $this->_parseOptions($options),
'tagValue' => $content
);
return $this->_assign('button', $values);
} }
/** /**
* Creates a submit button element. * Creates a submit button element.

View file

@ -69,6 +69,7 @@ class HtmlHelper extends AppHelper {
'file_no_model' => '<input type="file" name="%s" %s/>', 'file_no_model' => '<input type="file" name="%s" %s/>',
'submit' => '<input type="submit" %s/>', 'submit' => '<input type="submit" %s/>',
'submitimage' => '<input type="image" src="%s" %s/>', 'submitimage' => '<input type="image" src="%s" %s/>',
'button' => '<input type="%s" %s/>',
'image' => '<img src="%s" %s/>', 'image' => '<img src="%s" %s/>',
'tableheader' => '<th%s>%s</th>', 'tableheader' => '<th%s>%s</th>',
'tableheaderrow' => '<tr%s>%s</tr>', 'tableheaderrow' => '<tr%s>%s</tr>',

View file

@ -1153,6 +1153,30 @@ class FormHelperTest extends CakeTestCase {
$this->assertNoPattern('/<input[^<>]+[^(type|name|value|id)]=[^<>]*>$/', $result); $this->assertNoPattern('/<input[^<>]+[^(type|name|value|id)]=[^<>]*>$/', $result);
} }
function testButton() {
$result = $this->Form->button('Hi');
$expected = '<input type="button" value="Hi" />';
$this->assertEqual($result, $expected);
$result = $this->Form->button('Clear Form', array('type' => 'clear'));
$expected = '<input type="clear" value="Clear Form" />';
$this->assertEqual($result, $expected);
$result = $this->Form->button('Reset Form', array('type' => 'reset'));
$expected = '<input type="reset" value="Reset Form" />';
$this->assertEqual($result, $expected);
$result = $this->Form->button('Options', array('type' => 'reset', 'name' => 'Post.options'));
$this->assertPattern('/^<input type="reset" [^<>]+ \/>$/', $result);
$this->assertPattern('/^<input [^<>]+value="Options"[^<>]+\/>$/', $result);
$this->assertPattern('/^<input [^<>]+name="data\[Post\]\[options\]"[^<>]+\/>$/', $result);
$this->assertPattern('/^<input [^<>]+id="PostOptions"[^<>]+\/>$/', $result);
$result = $this->Form->button('Options', array('type' => 'reset', 'name' => 'Post.options', 'id' => 'Opt'));
$this->assertPattern('/^<input [^<>]+id="Opt"[^<>]+\/>$/', $result);
$this->assertNoPattern('/^<input [^<>]+id=[^<>]+id=/', $result);
}
function testSubmitButton() { function testSubmitButton() {
$result = $this->Form->submit('Test Submit'); $result = $this->Form->submit('Test Submit');
$this->assertPattern('/^<div\s+class="submit"><input type="submit"[^<>]+value="Test Submit"[^<>]+\/><\/div>$/', $result); $this->assertPattern('/^<div\s+class="submit"><input type="submit"[^<>]+value="Test Submit"[^<>]+\/><\/div>$/', $result);