Zend Framework 2 How to get an individual switch in the field of view of a script?

I am using Twitter Bootstrap 3. The HTML for the switches should look like this:

<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 the switches 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 can I separate each switch in a script view?

PS: Or any solution that will create as html code using a form.

EDIT:

Thanks to Sam. But I will leave these options for more complex cases. During the experiments, the following happened (with the standard view assistant):

 // 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, thanks to Sam for his help, I won’t understand without him.

+4
source share
2 answers

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

In addition, since you will probably need TB3-Style for all of your form elements, you might be interested in one of the many, many TB Modules

// Edit

I don’t see how the documentation does not answer your question: SI considers the default formRadio() viewHelper is what you are looking for anyway, 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 opportunity to write your own formRadio() ViewHelper, which writes a div for you. It is easy to find a SO question there, only for this very topic .

// Edit3

Feeling guilty about the relationship, your ViewHelper may 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 should 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 form of the Radio, not formMultiCheckbox. This is because I think TB3 will have a different CSS class for rendering checkboxes, not radio elements.

+4
source

My decision:

the 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 information, you can study the code below: ZF2 rendering of individual radio elements with helpers

+1
source

All Articles