Permalink
Browse files

Fix <symfony:input /> to select an radio, checkbox, or option

  • Loading branch information...
1 parent b3defcf commit 90386f4a267f64d2d0b0c16f4f4de296be35c6c1 @aizatto committed Jan 8, 2012
Showing with 17 additions and 32 deletions.
  1. +17 −32 src/xhp/symfony/input.php
@@ -69,7 +69,8 @@ protected function renderField() {
$this->transferFormViewAttributes($input)
->setAttribute('value', $formview->get('value'));
- if ($this->getAttribute('type') == 'checkbox' &&
+ $type = $this->getAttribute('type');
+ if (($type == 'checkbox' || $type == 'radio') &&
$formview->get('checked')) {
$input->setAttribute('checked', 'checked');
}
@@ -84,49 +85,25 @@ protected function renderChoice() {
$formview = $this->getAttribute('formview');
if ($formview->get('expanded')) {
- if ($formview->get('multiple')) {
- $content = $this->renderChoiceCheckbox();
- } else {
- $content = $this->renderChoiceRadio();
- }
-
return
<x:frag>
- {$content}
+ {$this->renderChoiceExpanded()}
</x:frag>;
} else {
return $this->renderChoiceSelect();
}
}
- protected function renderChoiceCheckbox() {
+ protected function renderChoiceExpanded() {
$formview = $this->getAttribute('formview');
- $choices = $formview->get('choices');
- $html = array();
- foreach ($choices as $value => $option) {
- $input = <input type="checkbox" value={$value} />;
- $this->transferFormViewAttributes($input);
- $html[] =
- <label>
- {$input}
- {$option}
- </label>;
- }
+ $children = $formview->getChildren();
- return $html;
- }
-
- protected function renderChoiceRadio() {
- $formview = $this->getAttribute('formview');
- $choices = $formview->get('choices');
$html = array();
- foreach ($choices as $value => $option) {
- $input = <input type="radio" value={$value} />;
- $this->transferFormViewAttributes($input);
+ foreach ($children as $child) {
$html[] =
<label>
- {$input}
- {$option}
+ <symfony:input formview={$child} />
+ {$child->get('label')}
</label>;
}
@@ -136,12 +113,20 @@ protected function renderChoiceRadio() {
protected function renderChoiceSelect() {
$formview = $this->getAttribute('formview');
$choices = $formview->get('choices');
+ $selected_value = $formview->get('value');
+
$options = array();
foreach ($choices as $value => $option) {
- $options[] =
+ $option =
<option value={$value}>
{$option}
</option>;
+
+ if ($selected_value == $value) {
+ $option->setAttribute('selected', true);
+ }
+
+ $options[] = $option;
}
$element =

0 comments on commit 90386f4

Please sign in to comment.