I'm developing a Joomla component. I created a form which contains a radio list so in the xml form I have:
<field name="testradio" type="radio" default="0" label="Select an option" description="">
<option value="0">Option 0</option>
<option value="1">Option 1</option>
</field>
I've created an edit view so I can set the value of testradio:
<?php $fields = $this->form->getFieldset($group); ?>
<?php if (count($fields)) : ?>
<?php foreach ($fields as $field) : ?>
<div class="control-group">
<div class="control-label">
<?php echo $field->label; ?>
</div>
<div class="controls">
<?php echo $field->input; ?>
</div>
</div>
<?php endforeach;?>
<?php endif;?>
I save the selected radio checkbox in the database. Now, I load the content from my database to consult view.
<?php foreach ($fields as $field) : ?>
<div><?php echo $field->value; ?></div>
<?php endforeach;?>
It displays "0" (in the case I chose Option 0) and I would like to show "Option 0". Is there a standard way to display the option text instead of the option value?
Finally I extended the JFormFieldRadio and added a method to get this value.
public function getOptionText($value) {
foreach ($this->element->option as $option) {
$optvalue = (string)$option['value'];
if ($optvalue === $value)
return (string)$option;
}
return '';
}