views:

241

answers:

3

Hi,

I'm trying to add Zend_Form_Elements following the radio buttons in my form, but so far I have been unable to do so. If anyone could point me in the right direction it would be greatly appreciated. Form needs to be rendered as shown below:

(*) [____]%

( ) [____]€

( ) [___] for [___]
A: 

Use form decorators and css to style the input as you want.

You woulduse a forn decorator to rewrite the markup for your form. But its quite a challenge franckly and maybe your best bet is a form viewScript. There very easy to implement and give you 100% control over the html for the form. Look at

Example: Full Customization Using the ViewScript Decorator

In the example I linked.

Iznogood
That's an incredibly vague answer. Care to be a bit more precise?
Andy
A: 

How about something like this:

class My_RadioForm extends Zend_Form
{
    public function init()
    {
        $this->addElement('radio', 'myradio', array(
            'label' => 'Select an option below',
            'multiOptions' => array(
                'val1'  => 'Text 1',
                'val2'  => 'Text 2',
                'val3'  => 'Text 2',
            ),
        ));
    }

}

For very specific rendering, @iznogood is right: gotta get into all that the decorator business.

David Weinraub
Hi there. This is pretty much the code I already have at my disposal. The issue is that I need to insert elements between the iterations, otherwise it would just be a plain list of radio buttons. If I need to get into decorators, what would I need to do? Add and/or remove decorators? Create my own? 'Use form decorators' is awfully unspecific.
Andy
A: 

You could use docorators if it is only a matter of positioning input tags. You can do that by overwriting/modifying default FormElements decorator, for example, by adding float: left style property to particular form element. Continuing from David's example:

 $this->addElement('radio', 'myradio', array(
        'label' => 'Select an option below',
        'multiOptions' => array(
            'val1'  => 'Text 1',
            'val2'  => 'Text 2',
            'val3'  => 'Text 2',
        ), 
        'decorators' =>
          array(
          'ViewHelper',
          'Errors',
          'Description',
          array('HtmlTag', array('tag' => 'dd', 'style' => 'float: left')),
          'Label'
        )
));

There is also a setDefaultDecorators() method, that allows you to overwrite all elements decorators in entire form.

More advanced solution if to create composite elements - http://weierophinney.net/matthew/archives/217-Creating-composite-elements.html - i.e. custom form element containing multiple input tags. You have more control over your form business logic that way.

c64ification
That's not what I want at all, actually. I want a method for inserting elements next to the radio buttons. The current implementation only allows for labels being inserted (see 'Text 1', 'Text 2' etc. in your example). Simply moving things about with CSS would destroy the natural flow of the page.
Andy
You should create then your own composite form elements.
c64ification