Permalink
Browse files

Refactoring `FormHelper` and adding test coverage.

  • Loading branch information...
1 parent 3c9c1b6 commit 5faf140a878b9d6e6d18ec0fb3b098d98fa7aca4 @nateabele nateabele committed Oct 19, 2009
View
0 app/tmp/logs/empty 100644 → 100755
No changes.
@@ -119,8 +119,10 @@ protected function _formatAttr($key, $value, $options = array()) {
$value = (string)$value;
if (in_array($key, $this->_minimized)) {
- $isMini = ($value === 1 || $value === true || $value === 'true' || $value == $key);
- $value = $isMini ? $key : $value;
+ $isMini = ($value == 1 || $value === true || $value === 'true' || $value == $key);
+ if (!($value = $isMini ? $key : $value)) {
+ return null;
+ }
}
if ($options['escape']) {
@@ -44,12 +44,12 @@ class Form extends \lithium\template\Helper {
'select-start' => '<select name="{:name}"{:options}>',
'select-multi-start' => '<select name="{:name}[]"{:options}>',
'select-empty' => '<option value=""{:options}>&nbsp;</option>',
- 'select-option' => '<option value="{:value}"{:options}>{:content}</option>',
+ 'select-option' => '<option value="{:value}"{:options}>{:title}</option>',
'select-end' => '</select>',
- 'submit' => '<input type="submit"{:options} />',
+ 'submit' => '<input type="submit" value="{:title}"{:options} />',
'submit-image' => '<input type="image" src="{:url}"{:options} />',
'text' => '<input type="text" name="{:name}"{:options} />',
- 'textarea' => '<textarea name="{:name}"{:options}>{:content}</textarea>',
+ 'textarea' => '<textarea name="{:name}"{:options}>{:value}</textarea>',
'fieldset' => '<fieldset{:options}>{:content}</fieldset>',
'fieldset-start' => '<fieldset><legend>{:content}</legend>',
'fieldset-end' => '</fieldset>'
@@ -91,8 +91,8 @@ public function __construct($config = array()) {
* Allows you to configure a default set of options which are included on a per-method basis,
* and configure method template overrides.
*
- * To force all `<label />` elements to have a default `class` attribute value of "foo"`, simply
- * do the following:
+ * To force all `<label />` elements to have a default `class` attribute value of `"foo"`,
+ * simply do the following:
*
* {{{
* $this->form->config(array('label' => array('class' => 'foo')));
@@ -120,59 +120,67 @@ public function config($config = array()) {
);
}
- public function create($modelName, $options = array()) {
- list($name, $options, $template) = $this->_defaults('form', $name, $options);
- return $this->_render(__METHOD__, $template, compact('name', 'options'));
- }
-
- public function submit($buttonText = null, $options = array()) {
- list($name, $options, $template) = $this->_defaults(__FUNCTION__, $name, $options);
- return $this->_render(__METHOD__, $template, compact('name', 'options'));
- }
-
- public function end($submitButtonText = null) {
- $output = '';
- if ($submitButtonText !== null) {
- $output = $this->submit($submitButtonText);
- }
- return $output.$this->_strings['form-end'];
+ public function submit($title = null, $options = array()) {
+ list($name, $options, $template) = $this->_defaults(__FUNCTION__, null, $options);
+ return $this->_render(__METHOD__, $template, compact('title', 'options'));
}
public function textarea($name, $options = array()) {
- $content = isset($options['value'])?$options['value']:null;
list($name, $options, $template) = $this->_defaults(__FUNCTION__, $name, $options);
- return $this->_render(__METHOD__, $template, compact('name', 'options', 'content'));
+ $value = isset($options['value']) ? $options['value'] : '';
+ unset($options['value']);
+ return $this->_render(__METHOD__, $template, compact('name', 'options', 'value'));
}
public function text($name, $options = array()) {
list($name, $options, $template) = $this->_defaults(__FUNCTION__, $name, $options);
return $this->_render(__METHOD__, $template, compact('name', 'options'));
}
- public function select($name, $options = array(), $htmlAttributes = array()) {
- $output = $this->_render(__METHOD__, 'select-start', array(
- 'name' => $name,
- 'options' => $htmlAttributes
- ));
-
- foreach ($options as $value => $content) {
- $optionAttribites = array();
- if (isset($htmlAttributes['value']) && $htmlAttributes['value'] == $value) {
- $optionAttribites['selected'] = 'selected';
- }
- $output .= $this->_render(__METHOD__, 'select-option', array(
- 'value' => $value,
- 'content' => $content,
- 'options' => $optionAttribites
+ /**
+ * Generates a `<select />` list using the `$list` parameter for the `<option />` tags. The
+ * default selection will be set to the value of `$options['value']`, if specified.
+ *
+ * For example: {{{
+ * $this->form->select('colors', array(1 => 'red', 2 => 'green', 3 => 'blue'), array(
+ * 'id' => 'Colors', 'value' => 2
+ * ));
+ * // Renders a '<select />' list with options 'red', 'green' and 'blue', with the 'green'
+ * // option as the selection
+ * }}}
+ *
+ * @param string $name The `name` attribute of the `<select />` element.
+ * @param array $list An associative array of key/value pairs, which will be used to render the
+ * list of options.
+ * @param array $options Any HTML attributes that should be associated with the `<select />`
+ * element. If the `'value'` key is set, this will be the value of the option
+ * that is selected by default.
+ * @return string Returns an HTML `<select />` element.
+ */
+ public function select($name, $list = array(), $options = array()) {
+ list($name, $options, $template) = $this->_defaults(__FUNCTION__, $name, $options);
+ $val = isset($options['value']) ? $options['value'] : null;
+ unset($options['value']);
+
+ $output = $this->_render(__METHOD__, 'select-start', compact('name', 'options'));
+ $base = $options;
+
+ foreach ($list as $value => $title) {
+ $options = ($val == $value) ? array('selected' => true) : array();
+ $output .= $this->_render(__METHOD__, 'select-option', compact(
+ 'value', 'title', 'options'
));
}
-
- $output .= $this->_strings['select-end'];
- return $output;
+ return $output . $this->_context->strings('select-end');
}
public function checkbox($name, $options = array()) {
list($name, $options, $template) = $this->_defaults(__FUNCTION__, $name, $options);
+
+ if (!isset($options['checked'])) {
+ $options['checked'] = isset($options['value']) ? $options['value'] : false;
+ }
+ unset($options['value']);
return $this->_render(__METHOD__, $template, compact('name', 'options'));
}
@@ -112,6 +112,73 @@ public function testLabelGeneration() {
));
}
+ public function testSubmitGeneration() {
+ $result = $this->form->submit('Continue >');
+ $this->assertTags($result, array('input' => array(
+ 'type' => 'submit',
+ 'value' => 'Continue &gt;'
+ )));
+
+ $result = $this->form->submit('Continue >', array('class' => 'special'));
+ $this->assertTags($result, array('input' => array(
+ 'type' => 'submit',
+ 'value' => 'Continue &gt;',
+ 'class' => 'special'
+ )));
+ }
+
+ public function testTextareaGeneration() {
+ $result = $this->form->textarea('foo', array('value' => 'some content'));
+ $this->assertTags($result, array(
+ 'textarea' => array('name' => 'foo'),
+ 'some content',
+ '/textarea'
+ ));
+ }
+
+ public function testCheckboxGeneration() {
+ $result = $this->form->checkbox('foo');
+ $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'foo')));
+
+ $result = $this->form->checkbox('foo', array('checked' => false));
+ $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'foo')));
+
+ $result = $this->form->checkbox('foo', array('checked' => true));
+ $this->assertTags($result, array('input' => array(
+ 'type' => 'checkbox', 'name' => 'foo', 'checked' => 'checked'
+ )));
+
+ $result = $this->form->checkbox('foo', array('value' => true));
+ $this->assertTags($result, array('input' => array(
+ 'type' => 'checkbox', 'name' => 'foo', 'checked' => 'checked'
+ )));
+ }
+
+ public function testSelectGeneration() {
+ $result = $this->form->select('foo');
+ $this->assertTags($result, array('select' => array('name' => 'foo'), '/select'));
+
+ $result = $this->form->select(
+ 'colors',
+ array('r' => 'red', 'g' => 'green', 'b' => 'blue'),
+ array('id' => 'Colors', 'value' => 'g')
+ );
+
+ $this->assertTags($result, array(
+ 'select' => array('name' => 'colors', 'id' => 'Colors'),
+ array('option' => array('value' => 'r')),
+ 'red',
+ '/option',
+ array('option' => array('value' => 'g', 'selected' => 'selected')),
+ 'green',
+ '/option',
+ array('option' => array('value' => 'b')),
+ 'blue',
+ '/option',
+ '/select'
+ ));
+ }
+
public function testTemplateRemapping() {
$result = $this->form->password('passwd');
$this->assertTags($result, array('input' => array(

0 comments on commit 5faf140

Please sign in to comment.