我将如何格式化 Zend_Form_Element_Radio 以便标签跟随输入? [英] How would I format Zend_Form_Element_Radio so the label follows the input?

查看:51
本文介绍了我将如何格式化 Zend_Form_Element_Radio 以便标签跟随输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Zend_Form_Element_Radio 的默认装饰器是

<label for="type_id-1"><input type="radio" name="type_id" id="type_id-1" value="1">Pack</label>

label 标签包裹了 input 标签.相反,我想看起来像

<input type="radio" name="type_id" id="type_id-1" value="1"><label for="type_id-1">Pack</label>

我认为这可能与元素的标签"有关,但那是不同的.即使使用以下代码,我仍然得到包裹收音机的标签.当我使用此表单代码时.

公共函数init(){父::init();$this->setName('createdomain');$type_id = new Zend_Form_Element_Radio('type_id');$type_id->setLabel('请选择')->setRequired()-> setMultiOptions(array('m' => "male", 'f' => 'female'));$this->addElement($type_id);$this->setElementDecorators(array('视图助手',数组('标签',数组('位置' => 'APPEND')),));}

结果我得到了这个 HTML

<form id="createdomain" enctype="application/x-www-form-urlencoded" action="" method="post"><dl class="zend_form"><label for="type_id-m"><input type="radio" name="type_id" id="type_id-m" value="m">male</label><br/><label for="type_id-f"><input type="radio" name="type_id" id="type_id-f" value="f">female</label><label for="type_id" class="required">请选择</label></dl></表单>

注意 label 标签如何包裹 input 标签?

解决方案

这是 jQuery 而不是 Zend Framework 的问题.标签标签中元素的包装是完全有效的,只是 jQuery UI 不支持它.我已经发布了错误报告.

* 更新答案 *

但是,我认为您尝试做的(如您所评论的)是使用 jQuery UI 按钮集,这正是我遇到 jQuery UI 错误时所做的.简而言之,在修复错误之前,您有两个选择:

1) 使用 Dennis D. 的自定义视图助手来覆盖默认的单选按钮元素.

2) 使用 Dennis D. 编写的代码修补 Zend Framework Radio 按钮视图助手.它出现在文件 Zend_View_Helper_FormRadio 的第 169 行(Zend 框架版本 1.11.0)中.

首先新建一个标签并关闭标签

//创建标签$label = '<标签'.$this->_htmlAttribs($label_attribs) .' for="' . $optId . '">'.(('prepend' == $labelPlacement) ? $opt_label : '').'<input type="' . $this->_inputType . '"'.$opt_label.'</标签>';

然后将创建单选按钮的代码更改为:

//创建单选按钮$radio = '<input type="' . $this->_inputType . '"'

在视图助手中第三次删除标签标签的关闭(正如您已经完成的那样),更改:

<预><代码>.$endTag.(('append' == $labelPlacement) ? $opt_label : '').'</标签>';

并简单地替换为:

<预><代码>.$endTag;

然后使用放置定位组合收音机和标签:

//结合标签和单选按钮if ('prepend' == $labelPlacement) {$radio = $label .$radio;} 别的 {$radio = $radio .$标签;}

就是这样(Dennis D 再次在视图助手中完成了它)但是更改后的代码应该看起来像(从第 169 行开始:

//创建标签$label = '<标签'.$this->_htmlAttribs($label_attribs) .' for="' . $optId . '">'.$opt_label.'</标签>';//创建单选按钮$radio = '<input type="' . $this->_inputType . '"'.' name="' . $name . '"'.' id="' . $optId . '"'.' value="' . $this->view->escape($opt_value) . '"'.$checked.$禁用.$this->_htmlAttribs($attribs).$endTag;//组合标签和单选按钮if ('prepend' == $labelPlacement) {$radio = $label .$radio;} 别的 {$radio = $radio .$标签;}//添加到单选按钮数组$list[] = $radio;

The default decorator for the Zend_Form_Element_Radio is

<label for="type_id-1"><input type="radio" name="type_id" id="type_id-1" value="1">Pack</label>

The label tag wraps the input tag. Instead I would like to to look like

<input type="radio" name="type_id" id="type_id-1" value="1"><label for="type_id-1">Pack</label>

I thought it might have to do with the "Label" of the element, but that is different. Even with the following code, I still get the label wrapping the radio. When I use this form code.

public function init()
{
    parent::init();

    $this->setName('createdomain');

    $type_id = new Zend_Form_Element_Radio('type_id');
    $type_id->setLabel('Please Choose')
            ->setRequired()
            ->setMultiOptions(array('m' => "male", 'f' => 'female'));

    $this->addElement($type_id);

    $this->setElementDecorators(array(
    'ViewHelper',
     array('Label',array('placement' => 'APPEND')),
));       
}

I get this HTML as a result

<form id="createdomain" enctype="application/x-www-form-urlencoded" action="" method="post"><dl class="zend_form">
<label for="type_id-m"><input type="radio" name="type_id" id="type_id-m" value="m">male</label><br />
<label for="type_id-f"><input type="radio" name="type_id" id="type_id-f" value="f">female</label>
<label for="type_id" class="required">Please Choose</label></dl>
</form>

Notice how there is a label tag wrapping the input tag?

解决方案

It's an issue with jQuery and not the Zend Framework. The wrapping of the element in the label tag is perfectly valid it's just jQuery UI doesn't support it. I have posted a bug report.

* Update Answer *

However I think what you are trying to do (as you commented) is to use the jQuery UI buttonset, which is what I was doing when I came across the jQuery UI bug. In short you have two options until the bug is fixed:

1) Use Dennis D.'s custom view helper to over ride the default radio button element.

2) Patch the Zend Framework Radio button view helper with the code Dennis D. has written. It appears in the file Zend_View_Helper_FormRadio on line 169 (Zend framework Version 1.11.0).

Firstly create a new label and close the tag

// Create the label
$label = '<label'
. $this->_htmlAttribs($label_attribs) . ' for="' . $optId . '">'
. (('prepend' == $labelPlacement) ? $opt_label : '')
. '<input type="' . $this->_inputType . '"'
. $opt_label
. '</label>';

Secondly alter the code that creates the radio button to:

// Create the radio button
$radio = '<input type="' . $this->_inputType . '"'

Thirdly remove the closing of the label tag (as you've already done it) in the view helper, change:

. $endTag
. (('append' == $labelPlacement) ? $opt_label : '')
. '</label>';

And simply replace with:

. $endTag;

Then combine the radio and the label using the placement positioning:

// Combine the label and the radio button
if ('prepend' == $labelPlacement) {
    $radio = $label . $radio;
} else {
    $radio = $radio . $label;
}

And that's it (again Dennis D has done it in the view helper) but the changed code should look like (starting at line 169:

// Create the label
        $label = '<label'
                . $this->_htmlAttribs($label_attribs) . ' for="' . $optId . '">'
                . $opt_label
                . '</label>';

        // Create the radio button
        $radio = '<input type="' . $this->_inputType . '"'
                . ' name="' . $name . '"'
                . ' id="' . $optId . '"'
                . ' value="' . $this->view->escape($opt_value) . '"'
                . $checked
                . $disabled
                . $this->_htmlAttribs($attribs)
                . $endTag;

        // Combine the label and the radio button
        if ('prepend' == $labelPlacement) {
            $radio = $label . $radio;
        } else {
            $radio = $radio . $label;
        }
        // add to the array of radio buttons
        $list[] = $radio;

这篇关于我将如何格式化 Zend_Form_Element_Radio 以便标签跟随输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆