多复选框的 Zend 表单从标签中删除输入 [英] zend form for multicheckbox remove input from labels

查看:22
本文介绍了多复选框的 Zend 表单从标签中删除输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 zend_form(Zend Framwork 的一部分)来创建一个表单,当我使用 Zend_Form 的多复选框元素(Zend_Form_Element_MultiCheckbox)添加一组复选框时发现输出的代码如下:

即输入在标签内.虽然代码在技术上是可以的,但就 W3c 而言,它违背了良好的做法,也不是我想要的方式,因为它使样式变得更加困难.我想要的是以下内容:

<label for="subjects-maths">数学<input type="checkbox" value="maths" id="subjects-maths" name="subjects[]">

<div><label for="subjects-english">英语<input type="checkbox" value="maths" id="subjects-english" name="subjects[]">

因此,如何将复选框输入移出标签,以及如何将 div 中的每个选项括起来.我在互联网上查看了许多所谓的解决方案页面,但没有一个适用于多复选框,因为它只针对周围的标签(即在本例中会说主题的标签),而不是实际的选项标签和输入.

请帮忙.

谢谢保罗

解决方案

看来最好的解决方案是基于 squirrel 的上述答案.由于标签内的输入是硬编码的,因此最好的解决方案是改变这种行为.您可以通过修改 Zend_View_Helper_FormRadio 类或用您自己的自定义视图标题覆盖它来完成此操作.我会推荐自定义标头选项,因为修改 zend 框架将是一个坏主意,因为每次更新框架时都需要更新,对使用该框架副本的任何人或任何项目产生影响等.通过使用自定义帮助程序将特定于项目,它不会影响其他任何事情,也不会受到更新框架的影响(但是,如果框架更新更改了某些行为,您可能会发现需要更新帮助程序).我所做的运行良好的是:

1 - 在库中创建了一个模型/模块,我调用了我的网站.这首先涉及在库中创建一个与模型同名的文件夹.因此我有图书馆>网站

忘了说您需要通过 bootstrap 或 application.ini 注册插件库.我发现这是最简单的添加:

autoloaderNamespaces[] = "网站_"

应用程序命名空间下的任何位置

2 - 我为类 Website_View_Helper_FormRadio 创建了相关的子文件夹和文件,它将覆盖 Zend_View_Helper_FormRadio.因此文件夹和文件结构是

website > view > helper > FormRadio.php

3 - 然后我将 formRadio 函数的内容从 Zend_View_Helper_FormRadio 复制到类中的 Website_View_Helper_FormRadio 中.然后我用松鼠引用的代码修改它,因此从 Zend_View_Helper_FormRadio 继承的 Website_View_Helper_FormRadio 类像这样:

class Website_View_Helper_FormRadio 扩展 Zend_View_Helper_FormRadio{public function formRadio($name, $value = null, $attribs = null, $options = null, $listsep = "<br/>
"){//Zend_View_Helper_FormRadio 中 formRadio 函数的代码//修改自松鼠引用的代码}}

然后,这为我们提供了我们自己的类副本,其中包含从 FormRadio 元素的 Zend 版本继承的修改后的代码.

您现在可以像平常一样使用 Zend Form Radio 元素,它将使用我们的改进

请注意,如果您希望对 MultiCheckbox 或复选框具有相同的效果,则需要使它们使用我们的表单收音机版本,因为这些元素使用表单收音机作为基础.为此,我们在我们的库中创建了我们自己的这些版本,并使它们从我们的版本继承.因此,对于 multicheckbox,我们需要以下内容:

library > website > view > helper > FormMultiCheckbox.php

将是 Zend_View_Helper_FormMultiCheckbox 的副本

然后替换该行:

class Zend_View_Helper_FormMultiCheckbox 扩展 Zend_View_Helper_FormRadio

与:

class Website_View_Helper_FormMultiCheckbox 扩展了 Website_View_Helper_FormRadio

我希望对某人有所帮助.

I am using zend_form (part of Zend Framwork) to create a form and found when I add a set of checkboxes using Zend_Form's multicheckbox element (Zend_Form_Element_MultiCheckbox) the code that is outputted like so:

<label for="subjects-maths">
    <input type="checkbox" value="maths" id="subjects-maths" name="subjects[]">
    Maths
</label>
<label for="subjects-english">
    <input type="checkbox" value="maths" id="subjects-english" name="subjects[]">
    English    
</label>

I.e. the input is inside the label. Whereas the code is technically ok it is against good practice as far as the W3c see it and is also not the way I want it as it makes styling harder. What I want is the following:

<div>
    <label for="subjects-maths">        
        Maths
    </label>
    <input type="checkbox" value="maths" id="subjects-maths" name="subjects[]">
</div>
<div>
    <label for="subjects-english">            
        English    
    </label>
    <input type="checkbox" value="maths" id="subjects-english" name="subjects[]">
</div>

Therefore how do I move the checkbox input out of the label and how do I surround each option in divs. I have looked at many pages on the internet of so called solutions but none work on multicheckboxes as it only targets surrounding labels (i.e the one that would say subjects in this example) not the actual options labels and inputs.

Please help.

Thanks Paul

解决方案

It appears the best solution is based on the answer above by squirrel. As the input being inside a label is hard coded the best solution is to change this behavour. You can either do this by modifying the Zend_View_Helper_FormRadio class or override it with your own custom view header. I would recommend the custom header option as modifying the zend framework would be a bad idea as it would need updating everytime you updated the framework, have an effect on anyone or any project using that copy of the framework etc. By using a custom helper it will be specific to the project and it will not affect anything else nor will it be affected by updating the framework (however you may find you need to update the helper if the framework update has changed some behaviours). What I did which is working well is:

1 - Created a model/module in the library, I called mine website. This first involves creating a folder in the library the same name as the model. Thus I have library > website

Edit: Forgot to say you will need to register the plugin library either via the bootstrap or application.ini. I find it is easiest just add:

autoloaderNamespaces[] = "Website_"

Anywhere below appnamespace

2 - I created the relevant subfolders and file for the class Website_View_Helper_FormRadio which will override Zend_View_Helper_FormRadio. Thus the folder and file stucture is

website > view > helper > FormRadio.php

3 - I then copied the contents of the formRadio function from Zend_View_Helper_FormRadio into Website_View_Helper_FormRadio within the class. I then modified it with the code that squirrel referred thus the Website_View_Helper_FormRadio class that inherits from Zend_View_Helper_FormRadio like so:

class Website_View_Helper_FormRadio extends Zend_View_Helper_FormRadio
{
     public function formRadio($name, $value = null, $attribs = null, $options = null, $listsep = "<br />
")
    {

          // The code from the formRadio function in Zend_View_Helper_FormRadio with
          // the modification from the code squirrel referred to

    }
}

This then gives us our own copy of the class with our modified code that inherits from the zend version of the FormRadio element.

You can now just use the Zend Form Radio element like normal and it will use our improvements

Note if you wish to have the same effect on MultiCheckbox or checkbox you need to make these use our version of form radio as these elements use form radio as a basis. To do this we create our own versions of these in our library and make them inherit from our version. We would therefore have for multicheckbox the following:

library > website > view > helper > FormMultiCheckbox.php

Would be a copy of Zend_View_Helper_FormMultiCheckbox

Then replace the line:

class Zend_View_Helper_FormMultiCheckbox extends Zend_View_Helper_FormRadio

with:

class Website_View_Helper_FormMultiCheckbox extends Website_View_Helper_FormRadio

I hope that helps someone.

这篇关于多复选框的 Zend 表单从标签中删除输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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