Reputation:
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.
Upvotes: 4
Views: 7418
Reputation: 11
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
Upvotes: 1
Reputation: 16455
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.
Upvotes: 4