【问题标题】:How to wrap radio button group in label zend form如何以标签zend形式包装单选按钮组
【发布时间】:2013-12-28 08:06:39
【问题描述】:

我想要下面的带有两个 zend 形式的单选按钮组的 html

        <label class="inline">
        <input type="radio" name="form-field-radio">
    <span class="lbl"> Male</span>
    </label>
        &nbsp; &nbsp; &nbsp;
   <label class="inline">
    <input type="radio" name="form-field-radio">
    <span class="lbl"> Female</span>
   </label>

我正在使用以下代码在 Zend Form 中制作此按钮

$gender = $this->CreateElement('radio','gender')
        ->addFilter(new Zend_Filter_StringTrim())
        ->setMultiOptions(array('M'=>'Male', 'F'=>'Female'))
        ->setDecorators(array( array('ViewHelper') ));

但我不知道在这段代码中在哪里设置标签和跨度类。

请帮忙。

谢谢。

【问题讨论】:

    标签: zend-framework zend-form radio-group


    【解决方案1】:

    我不确定它是否是我第一次使用 zend 框架的完美方法,但如果你觉得有用的话,这里仍然是我所做的步骤:

    首先,我创建了一个扩展 Zend_Form_Decorator_Abstract 的自定义装饰器,并将其保存在位置 'decorator/My_Form_Decorator.php' 中。 decorator 是我在 root 创建的一个目录。

    .
     /decorator
     /application 
    

    等等……

    然后我将它包含在控制器中。我已经读到有某些方法可以添加像addPrefixPath() 这样的装饰器,但为了时间,我只是将装饰器文件与'include "../decorator/My_Form_Decorator.php";' 一起包含在顶部。然后我没有使用CreateElement 方法,而是使用Zend_Form_Element

    以下是自定义收音机装饰器的代码

    My_Form_Decorator.php

    class My_Decorator_RadioInput extends Zend_Form_Decorator_Abstract
    {
    
    
        public function render($content)
        {
            $element = $this->getElement();
    
            $label   = htmlentities($element->getLabel());
            $type = $element->type;
            $name = $element->elemName;
            $multiOptions = $element->multiOptions;
            $labelClass = $element->labelClass;
            $spanClass = $element->spanClass;
            $markup='';
    
            if(!empty($type) && !empty($name) && !empty($multiOptions)  && is_array($multiOptions)){
             foreach($multiOptions as $key=>$value){
               $markup .='<label class="'.$labelClass.'"><input type="radio" name="'.$name.'" value="'.$key.'"> <span class="'.$spanClass.'">'.$value.'</span></label>';                    
             }
            }
            return $markup;
        }
    }
    

    这是我的控制器函数中的代码

    IndexController.php

    $decorator = new My_Decorator_RadioInput();
    $form = new Zend_Form();
    $form->setAttrib('id', 'test');
    $element   = new Zend_Form_Element('foo', array(
        'elemName'=>'gender',
        'type' =>'radio',
        'multiOptions' => array('M'=>'Male', 'F'=>'Female'),
        'labelClass'=>'inline',
        'spanClass'=>'lbl',
        'decorators' => array($decorator),
    ));
    $form->addElement($element);
    $this->view->form = $form;
    

    在视图中 index.phtml

    echo $this->form
    

    希望这对你有帮助..

    【讨论】:

    • 它在页面上将“Male”打印为“Male”。
    • 所以他们正在转义html标签,它添加的标签类呢?
    • 让我尝试安装zend。如果成功我会通知你
    • 是的,我有。它对我有用。禅1.12。尝试时你的输出是什么?
    • 嗨 Nouphal,我逐行尝试了您的代码,它运行良好。现在你能建议我如何在你的代码中设置默认值 Male 吗??
    猜你喜欢
    • 1970-01-01
    • 2010-10-19
    • 2015-08-17
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    相关资源
    最近更新 更多