4

I'm using Twitter Bootstrap 3. Html code for the radio buttons should look like:

<div class="radio">
    <label>
        <input type="radio" name="search_text_source" value="title" />
        In the title
    </label>
</div>
<div class="radio">
    <label>
        <input type="radio" name="search_text_source" value="content" />
        In the content
    </label>
</div>
...

In the form, I create radio buttons as follows:

$this->add(array(
    'name' => 'search_text_source',
    'type' => 'radio',
    'options' => array(
        'value_options' => array(
            'title' => 'In the title',
            'content' => 'In the content',
            'description' => 'In the description',
        ),
    ),  
));

How do I get separate each radio button in view script?

P.S.: Or any decision, which will create like html code using the form.

EDIT:

Thanks to Sam figured out. But I will leave these variants for more complex cases. During the experiments, the following happened (with standart view helper):

// in view script:
<?
    $this->formRadio()->setSeparator('</div><div class="radio">');
?>

<!-- some html -->

<div class="radio">
    <?php echo $this->formRadio($form->get('search_text_source')) ?>
</div>

Once again, thank Sam for help, without him I would not understand.

2 Answers2

4

Does anyone ever read the documentation? There's even a dedicated chapter for Zend\Form\View\Helper-Classes, that should answer all your questions.

Furthermore, since you'd probably want the TB3-Style for all your Form Elements, you may be interested in one of the many, many TB-Modules

// Edit

I don't see how the documentation does not answer your question :S I believe the default output of the formRadio() viewHelper is what you're looking for anyways, so all you need is a separate div, right?

// any.phtml
<?=$this->form()->openTag($form);?>
<div class="radio">
    <?=$this->formRadio($form->get('radio1')); ?>
</div>
<?=$this->form()->closeTag();?>

// Edit2

And of course you always have the option of writing your very own formRadio() ViewHelper that writes out the div for you. There's an easy to find SO Question, just for this very topic available.

// Edit3

Feeling guilty in attitude, your ViewHelper could be as simple as this:

// Application\Form\View\Helper\FormRadio.php
namespace Application\Form\View\Helper;

use Zend\Form\View\Helper\FormRadio as OriginalFormRadio;

class FormRadio extends OriginalFormRadio
{
    protected function renderOptions(/* include original attributes here */)
    {
        // Include 100% of Original FormMultiCheckbox Code
        // https://github.com/zendframework/zf2/blob/master/library/Zend/Form/View/Helper/FormMultiCheckbox.php#L147

        // Now change this one line #227 into your code
        // Instead of: https://github.com/zendframework/zf2/blob/master/library/Zend/Form/View/Helper/FormMultiCheckbox.php#L227
        $template = '<div class="radio">' . $labelOpen . '%s%s' . $labelClose . '</div>';
    }
}

Then you gotta overwrite the default ViewHelper

// Inside Module Class
public function getViewHelperConfig()
{
    return array(
        'invokables' => array(
            'formradio' => 'Application\Form\View\Helper\FormRadio'
        )
    );
}

Additional Information: In this example i overwrite the original formRadio, rather than formMultiCheckbox. This is because I imagine TB3 would have a different CSS Class for rendering Checkboxes and not Radio elements.

Community
  • 1
  • 1
Sam
  • 16,435
  • 6
  • 55
  • 89
  • @dphn Added an example now, and yes, that IS documented. – Sam Sep 21 '13 at 11:05
  • Again you do not understand the question. I would not want to upset you, but this is what I have done in the first place. Your example code displays the entire **group of radio buttons** in a separate "div". A Twitter Bootstrap 3 requires that **each radio button** is in a separate "div". –  Sep 21 '13 at 11:19
  • @dphn Excuse my language - it's not meant as offensive as it sounds :S I was actually mistaken in the asumption that formRadio would render only one radio element. didnt realize it extends multicheckbox already :S In this case what i pointed out in **Edit2** is what you'd be after. You would have to write your own viewhelper – Sam Sep 21 '13 at 11:48
  • Thank you probably will. –  Sep 21 '13 at 12:00
  • @dphn Added an third edit - explaining in lil more detail what would have to be done - it's a matter of 2 minutes max ;) – Sam Sep 21 '13 at 12:37
1

My solution is:

Form:

$this->add(array(
'type'  => 'radio',
'name'  => 'gender',
'options' => array(
    'label' => 'Gender',
    'value_options' => array(
        'female' => array(
            'value' => '1',
         ),
         'male' => array(
             'value' => '2',

         ),
     ),
 ),));

View:

<div class="form-group">
<?php
$form->get('gender')->setLabelAttributes(array('class' => 'col-sm-4'));
echo $this->formLabel($form->get('gender'));
?>
<div class="col-lg-8">
    <?php
    $element = $form->get('gender');
    $options = $element->getOptions();
    $options = $options['value_options'];
    ?>
    <div class="rdio rdio-primary">
        <input id="female" type="radio" <?php if ($element->getValue() == $options['female']['value']) echo 'checked="checked"' ?> name="<?php echo $element->getName() ?>" value="<?php echo $options['female']['value'] ?>">
        <label for="female">Female</label>
    </div>
    <div class="rdio rdio-primary">

        <input id="male"  type="radio" <?php if ($element->getValue() == $options['male']['value']) echo 'checked="checked"' ?> name="<?php echo $element->getName() ?>" value="<?php echo $options['male']['value'] ?>">
        <label for="male">Male</label>
    </div>
</div>

For more details you can study the code given on : ZF2 rendering individual radio elements with helpers

Ana Hdez
  • 11
  • 2