Permalink
Browse files

Allow field options to be defined in an array of fields

  • Loading branch information...
1 parent e8112ad commit 35a409a9c3210c1d96e06bdd6676a52f9c90ff6b @Howard3 Howard3 committed Dec 8, 2011
Showing with 43 additions and 4 deletions.
  1. +13 −4 template/helper/Form.php
  2. +30 −0 tests/cases/template/helper/FormTest.php
View
@@ -448,11 +448,20 @@ protected function _fields(array $fields, array $options = array()) {
$result = array();
foreach ($fields as $field => $label) {
- if (is_numeric($field)) {
- $field = $label;
- unset($label);
+ $opts = $options;
+ switch (true) {
+ case is_numeric($field):
+ $field = $label;
+ unset($label);
+ break;
+ case is_array($label):
+ $opts = $label + $opts;
+ break;
+ default:
+ $opts['label'] = $label;
+ break;
}
- $result[] = $this->field($field, compact('label') + $options);
+ $result[] = $this->field($field, $opts);
}
return join("\n", $result);
}
@@ -822,6 +822,36 @@ public function testMultipleFields() {
));
}
+ public function testMultipleFieldsWithOptions() {
+ $result = $this->form->field(array(
+ 'username', 'password' => array('type' => 'password'), 'email' => 'E-mail',
+ 'gender' => array('type' => 'select', 'list' => array('male', 'female'))
+ ));
+ $this->assertTags($result, array(
+ array('div' => array()),
+ array('label' => array('for' => 'Username')), 'Username', '/label',
+ array('input' => array(
+ 'type' => 'text', 'name' => 'username', 'id' => 'Username'
+ )), '/div',
+ array('div' => array()),
+ array('label' => array('for' => 'Password')), 'Password', '/label',
+ array('input' => array(
+ 'type' => 'password', 'name' => 'password', 'id' => 'Password'
+ )), '/div',
+ array('div' => array()),
+ array('label' => array('for' => 'Email')), 'E-mail', '/label',
+ array('input' => array(
+ 'type' => 'text', 'name' => 'email', 'id' => 'Email'
+ )), '/div',
+ array('div' => array()),
+ array('label' => array('for' => 'Gender')), 'Gender', '/label',
+ array('select' => array('name' => 'gender', 'id' => 'Gender')),
+ array('option' => array('value' => 0, 'selected' => 'selected')), 'male', '/option',
+ array('option' => array('value' => 1)), 'female', '/option',
+ '/select', '/div'
+ ));
+ }
+
public function testCustomInputTypes() {
// Creates an HTML5 'range' input slider:
$range = $this->form->range('completion', array('min' => 0, 'max' => 100));

0 comments on commit 35a409a

Please sign in to comment.