zend form for multicheckbox从标签中移除输入 [英] zend form for multicheckbox remove input from labels

查看:126
本文介绍了zend form for multicheckbox从标签中移除输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 < label for =subjects-maths> 
< input type =checkboxvalue =mathsid =subjects-mathsname =subjects []>
数学
< / label>
< label for =subjects-english>
< input type =checkboxvalue =mathsid =subjects-englishname =subjects []>
英文
< / label>

I。输入在标签内。尽管代码在技术上是可行的,但就W3c的看法而言,这是违背良好做法的,并且也不是我想要的方式,因为它使得样式更难。我想要的是:

 < div> 
< label for =subjects-maths>
数学
< / label>
< input type =checkboxvalue =mathsid =subjects-mathsname =subjects []>
< / div>
< div>
< label for =subjects-english>
英文
< / label>
< input type =checkboxvalue =mathsid =subjects-englishname =subjects []>
< / div>

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



请帮助。



感谢
Paul

解决方案

看起来最好的解决方案是基于松鼠上面的答案。由于标签内部的输入是硬编码,所以最好的解决办法是改变这种行为。您可以通过修改Zend_View_Helper_FormRadio类或使用您自己的自定义视图头来覆盖它。我建议自定义标题选项作为修改zend框架将是一个坏主意,因为它需要更新每次你更新框架,对任何人或使用该框架副本的任何项目的影响等。通过使用自定义帮手它将专门针对该项目,它不会影响其他任何内容,也不会受到更新框架的影响(但是如果框架更新已经改变了某些行为,您可能会发现需要更新帮助程序)。我所做的工作很好:

<1> - 在库中创建了一个模型/模块,我称之为我的网站。首先涉及在库中创建一个与模型名称相同的文件夹。因此,我有库>网站



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

autoloaderNamespaces [] =网站_



p>

<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 extends Zend_View_Helper_FormRadio $ 
公共函数formRadio($ name,$ value = null,$ attribs = null,$ options = null,$ listsep =< br /> \\\

{

// Zend_View_Helper_FormRadio中带有
的formRadio函数中的代码//代码松鼠的修改引用

}
}

然后,我们使用修改过的代码为我们提供了自己的副本,该代码从FormRadio元素的zend版本继承。

你现在可以像普通的一样使用Zend Form Radio元素,它会使用我们的改进



注意if你希望在MultiCheckbox上有相同的效果,或者你需要使用复选框来使这些元素使用我们的表单收音机版本,因为这些元素使用表单收音机作为基础。要做到这一点,我们在我们的库中创建我们自己的版本,并使它们继承我们的版本。因此,我们需要为multicheckbox准备以下内容:

library> website> view> helper> FormMultiCheckbox.php

将是Zend_View_Helper_FormMultiCheckbox的副本

然后替换这行:

  



<$ p

$ p> class Website_View_Helper_FormMultiCheckbox extends Website_View_Helper_FormRadio

我希望能帮助别人。 p>

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 />\n")
    {

          // 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 form for multicheckbox从标签中移除输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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