3

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 '';
}
0

In you foreach statement add a conditional that checks against your $field->input and outputs what you want instead. Example below:

<?php if(strpos($field->label,'My Radio Group') !== false){?>
    <?php echo 'Option '.$field->value;?>
<?php }else{?>
    <?php echo $field->value;?>
<?php }?>

Just understand that doing it this way will cause your data value to be submitted back to your controller as "Option 0", you will need to therefore override your controller's save method to strip that "Option " string back out of the posted radio group value.

  • Thanks but for the advice about the save but I want to display the option text on a consult view. The edit view is OK. Plus, your code does not solve the situation. It can be a workaround only in the case where I have "$value=Option $value". I could have "0=My first choice". In this case, it does not work. – titchagcreation Mar 13 '16 at 9:11
0

I think what you done wrong is that default="0" in your xml file. Instead you try the radio field in the xml file as below. Few fields helps in xml to get what yo want.

<field  name="testradio" type="radio" label="Select an option"
    description="" class="btn-group btn-group-yesno">
    <option value="0">Option 0</option>
    <option value="1">Option 1</option>
</field>

I am sure when you have edit layout that time you automatically get the Option 0 selected if you chosen 0 previously.

Output in Edit Layout will be

enter image description here

Lets try this. If still any problem then feel free to say.

  • Sorry but not sure that you really understood my problem. I had no problem with the edit view nor with the selected value. My problem was to show the selected value in a consult view. I finally found a solution and gave the answer at the end of the question. – titchagcreation Dec 31 '16 at 9:37
0

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 '';
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.