4

我正在使用 Twitter Bootstrap 3。单选按钮的 Html 代码应如下所示:

<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>
...

在表单中,我创建如下单选按钮:

$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',
        ),
    ),  
));

如何在视图脚本中分离每个单选按钮?

PS:或任何决定,这将使用表单创建类似 html 代码。

编辑:

感谢山姆想通了。但我会将这些变体留给更复杂的情况。在实验期间,发生了以下情况(使用标准视图助手):

// 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>

再次感谢 Sam 的帮助,没有他我无法理解。

4

2 回答 2

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.

于 2013-09-21T10:37:38.213 回答
1

我的解决方案是:

形式:

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

         ),
     ),
 ),));

看法:

<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>

有关更多详细信息,您可以研究以下代码: ZF2 rendering individual radio elements with helpers

于 2015-04-27T15:17:39.400 回答