Putting some HTML next to a radio button in Zend Form
Asked Answered
T

2

5

I have built a form builder, and I need the ability to assign description to every option in a radio element. Upon creation of this radio element, I ask the user to assign each value a description, which should be shown next to the element. So, in my database, I have a value, a title and a description for each option in this radio element.

The question: How do I put something next to a radio element? Do I have to create a new way for the form to render radio elements, or can it be done via decorators?

Any little bit helps, useful reading is welcome too!

A representation of what i need:

**A title of the Radio element**
      O Option 1       -    A description for option 1    
      O Option 2       -    A description for option 2    
      O Option 3       -    A description for option 3    
      O Option 4       -    A description for option 4    

Any ideas?

Tootle answered 13/12, 2010 at 19:39 Comment(0)
S
12

It's hard to tell from your question but where abouts is the actual radio button in your markup?

If it's to the left or right of the option label, you can simply use the label to enter your extra markup and set the ViewHelper's escape property to false to enable HTML


For example

$form->addElement('radio', 'test', array(
    'label' => '**A title of the Radio element**',
    'multiOptions' => array(
        1 => 'Option 1 <strong>- A description for option 1</strong>',
        2 => 'Option 2 <strong>- A description for option 2</strong>'
    ),
    'escape' => false
));

Looks like this

Form Screenshot

Silicle answered 13/12, 2010 at 22:25 Comment(6)
The radio button itself is in a form. And what I want to do is have a description for the options, not the radio element itself. I don't think that there is such a thing as an option label, there is only a value and a title there. I don't even think there are option decorators, since the seperators ir done by a tag in the class. I thought that there could be a way to use the seperator for my purposes, but I don't think that will do.Tootle
@Janis Each radio button option has a value and label. These are set via key => value pairs when adding the multi options. You can position the option label before (prepend) or after (append) the radio button element as well as inform the ViewHelper decorator that you do not want to escape markup in the labels.Silicle
Ok, this sounds promising. Could you please provide some sort of an example, of how I could achieve this? Thanks!Tootle
@Janis I've added an example to my answerSilicle
Thank you so much! This is exactly what I was looking for. And spot on, with such minimal code changes. Thank you!Tootle
@Phil: Thank you very much for "escape => false" - that's what I was looking for :)Hooker
R
0

Check out the tutorial I wrote solving this problem.

http://wesleyalmeida.net/what-ive-learned/how-to-decorate-a-radio-element-in-zend/

Rhaetia answered 14/12, 2010 at 22:49 Comment(2)
In your case (complex markup), I would have gone with a custom decorator and / or view helper.Silicle
I haven't seen a good custom decorator that has worked for radio group elements. Being able to add HTML to the label has been the best option and easier to work with than decorators that are poorly documented. Do you know where there is a good tutorial on radio element decorators? The documentation on it is horrible!Rhaetia

© 2022 - 2024 — McMap. All rights reserved.