Zend framework 如何格式化Zend_Form_Element_Radio,使标签跟随输入?
Zend_Form_元素_Radio的默认装饰器为Zend framework 如何格式化Zend_Form_Element_Radio,使标签跟随输入?,zend-framework,radio-button,zend-form,Zend Framework,Radio Button,Zend Form,Zend_Form_元素_Radio的默认装饰器为 <label for="type_id-1"><input type="radio" name="type_id" id="type_id-1" value="1">Pack</label> 我认为这可能与元素的“标签”有关,但这是不同的。即使使用下面的代码,我仍然可以得到包裹收音机的标签。当我使用这个表单代码时 public function init() { parent::init();
<label for="type_id-1"><input type="radio" name="type_id" id="type_id-1" value="1">Pack</label>
我认为这可能与元素的“标签”有关,但这是不同的。即使使用下面的代码,我仍然可以得到包裹收音机的标签。当我使用这个表单代码时
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')),
));
}
结果我得到了这个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">Please Choose</label></dl>
</form>
男性
女性的
请选择
请注意,有一个标签
标记包裹着输入
标记?试试:
$this->setElementDecorators(array(
'ViewHelper',
array('Label',array('placement' => 'APPEND')),
));
看看它真的很棒。ZF的装饰程序可能非常复杂,但这段视频确实很好地解释了这一点。我创建了一个名为MyLib\u view\u helper\u FormRadio的自定义视图辅助程序(因此,如果引导执行此操作,它会自动调用),并重写和更改了第160行的Zend\u view\u helper\u FormRadio::FormRadio()方法(版本1.11),创建单选按钮的位置
$label=''
. $opt_标签
.'';
//用标签把收音机包起来
$radio='这是jQuery的问题,而不是Zend框架的问题。标签标记中元素的包装是完全有效的,只是jqueryui不支持它。我已经发了一封信
*更新答案*
然而,我认为您试图做的(正如您所评论的)是使用jQueryUI按钮集,这就是我遇到jQueryUI错误时正在做的。简而言之,在修复错误之前,您有两个选择:
1) 使用Dennis D.的自定义视图辅助对象覆盖默认单选按钮元素
2) 用Dennis D.编写的代码修补Zend Framework单选按钮视图帮助器。它出现在第169行的Zend_View_Helper_FormRadio文件中(Zend framework版本1.11.0)
首先创建一个新标签并关闭标签
// Create the label
$label = '<label'
. $this->_htmlAttribs($label_attribs) . ' for="' . $optId . '">'
. (('prepend' == $labelPlacement) ? $opt_label : '')
. '<input type="' . $this->_inputType . '"'
. $opt_label
. '</label>';
然后使用放置定位组合收音机和标签:
// Combine the label and the radio button
if ('prepend' == $labelPlacement) {
$radio = $label . $radio;
} else {
$radio = $radio . $label;
}
就是这样(Dennis D在view helper中也这样做了),但是更改后的代码应该是这样的(从第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;
//创建标签
$label=''
.$opt_标签
. '';
//创建单选按钮
$radio='到目前为止,我最好的想法可能是在jQuery中更改ZF[Zend Framework]HTML代码,以便它与jQuery UI格式兼容
以下是解决方案:
在ZF表单构造中:
$this->setElementDecorators(array(
'ViewHelper',
'Errors',
array(array('rowElement'=>'HtmlTag'), array('tag'=>'div', 'class'=>'element')),
array('Label', array('class'=>'first')),
));
这里最重要的是class=element的div,它将包装所有输入[以便在jQuery中很容易找到它们]
下面是JS代码:
$(function() {
$( "div.element > label" ).each(function() {
$('input', this).after('<label for="'+$(this).attr('for')+'"></label>');
$('label', this).text($(this).text());
var input = $('input', this).detach();
var label = $('label', this).detach();
var parent = $(this).parent();
$(this).remove();
input.appendTo(parent);
label.appendTo(parent);
});
$( "div.element" ).buttonset();
});
$(函数(){
$(“div.element>label”)。每个(函数(){
$('input',this.),在('')之后;
$('label',this.text($(this.text());
var input=$('input',this).detach();
var label=$('label',this).detach();
var parent=$(this.parent();
$(this.remove();
输入。追加到(父项);
标签。附加到(父项);
});
$(“div.element”).buttonset();
});
这是你能做的最好的事情,我在经历了很多痛苦之后才发现:))
$radios=新Zend_表单_元素_无线电(“通知”);
$notificationTypesArray=array();
foreach($key=>$value的notificationTypes){
$notificationTypesArray[$key]=$value
$radios->removeDecorator('DtDdWrapper');
$radios->removeDecorator('HtmlTag');
$radios->setSeparator(“”);
$radios->setDecorators(阵列)(
“ViewHelper”,
“错误”,
数组(数组('data'=>'HtmlTag')、数组('tag'=>'td','class'=>'notification\u type\u row')),
数组('Label',数组('tag'=>'span')),
数组(数组('row'=>'HtmlTag'),数组('tag'=>'tr')),
);
$radios->addMultiOptions($notificationTypesArray);
$this->addElement($radios);
我在同一主题的不同帖子上的回答应该可以帮助你你能用CSS做吗?我正在尝试使用jQuery UI的buttonset,它期望标签跟随输入标签,而不是包含在标签标签标签中。对不起,这也不是问题。因此,Zend只是在两个收音机之后附加另一个标签。女孩
男孩进行选择我确信答案在于为无线电元素指定不同的ViewHelper,但我不确定如何执行。不过谢谢您的帮助。@nvoyageur您应该发布更多的代码,因为我在答案中发布的内容非常有效,完全符合您在问题中提出的要求。因此,一定有其他问题。发布您的整个表单?我已经更新了问题,以包括我正在使用的表单代码和结果HTML。您会注意到,输入
标记被一个标签
标记包围,然后两个单选按钮后面都有一个按钮集标签。我希望您的问题记住今晚下班回家时查看它。我在输入和标签的格式方面,jQuery应该更灵活一些,但是我对编辑ZF装饰器比深入jQuery UI更自信。
. $endTag;
// Combine the label and the radio button
if ('prepend' == $labelPlacement) {
$radio = $label . $radio;
} else {
$radio = $radio . $label;
}
// 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;
$this->setElementDecorators(array(
'ViewHelper',
'Errors',
array(array('rowElement'=>'HtmlTag'), array('tag'=>'div', 'class'=>'element')),
array('Label', array('class'=>'first')),
));
$(function() {
$( "div.element > label" ).each(function() {
$('input', this).after('<label for="'+$(this).attr('for')+'"></label>');
$('label', this).text($(this).text());
var input = $('input', this).detach();
var label = $('label', this).detach();
var parent = $(this).parent();
$(this).remove();
input.appendTo(parent);
label.appendTo(parent);
});
$( "div.element" ).buttonset();
});
$radios = new Zend_Form_Element_Radio('notifications');
$notificationTypesArray = array();
foreach ($notificationTypes as $key => $value) {
$notificationTypesArray[$key] = $value
$radios->removeDecorator('DtDdWrapper');
$radios->removeDecorator('HtmlTag');
$radios->setSeparator('</td></tr><tr><td class="notification_type_row">');
$radios->setDecorators(array(
'ViewHelper',
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'notification_type_row')),
array('Label', array('tag' => 'span')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
);
$radios->addMultiOptions($notificationTypesArray);
$this->addElement($radios);