class Form_Select extends Form_Input { protected $choices; public function __construct($name, $form) { parent::__construct($name, $form); $this->choices = array(); } public function choices($array) { if (!is_array($array)) { $array = func_get_args(); } $this->choices = $array; return $this; } public function __toString($tab = '') { $this->_generate_class(); $id = ''; $label = ''; if (!empty($this->label)) { list($for, $id) = self::_generate_for_id($this->form->auto_id(), $this->attrs['name']); $label = '<label'.$for.'>'.$this->label.$this->form->label_suffix().'</label>'."\n$tab"; } $errors = $this->error_messages->__toString($tab); if (!empty($errors)) { $errors = "\n".$errors; } $value = $this->form->get_bounded_data($this->attrs['name']); $value = (!empty($value)) ? $value : $this->value; $j = 0; $fields = array(); foreach($this->choices as $v => $c) { if (is_array($c)) { $fields[] = "$tab\t".'<optgroup label="'.htmlspecialchars($v).'">'; foreach($c as $vv => $cc) { $selected = ''; if ($value $vv) { $selected ' selected="selected"'; } $fields[] = "$tab\t\t".'<option value="'.htmlspecialchars($vv).'"'.$selected.'> '.$cc.'</option>'; } $fields[] = "$tab\t".'</optgroup>'; } else { $selected = ''; if ($value $v) { $selected ' selected="selected"'; } $fields[] = "$tab\t".'<option value="'.htmlspecialchars($v).'"'.$selected.' /> '.$c.'</option>'; } } $field = '<select'.$id.$this->attrs.'>'."\n".implode("\n", $fields)."\n$tab</select>"; return $tab.sprintf("%2\$s%1\$s%3\$s", $field, $label, $errors); } }