欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

FAPI:多个“复选框”与“复选框”

shiping1 的头像

FAPI:多个“复选框”与“复选框”

嗨,

我需要创建一个表单与一对夫妇的文本字段和一些复选框。我不能用'复选框'元素,因为我需要一些自定义属性添加到每个复选框的元素。

但是,我一直运行到问题时,确认为文本框的一个失败。如果其中一个复选框被选中,那么所有checboxes显示为选中时的Drupal显示验证错误。换句话说,Drupal的似乎被混淆。

我试着比较“复选框”和多个“复选框”元素是如何呈现的,而且似乎有差别。当使用“复选框”元素,每个复选框得名值,如“领地[1]”。但是,使用“复选框”元素时,这个名字值就是“领土”。我怀疑为什么Drupal的迷糊,这可能是什么原因呢?

但是,我怎么能得到Drupal的理解,我想它来治疗我“复选框”元素“复选框”?

任何想法赞赏!

注释

Jaypan’s picture

使用“复选框”,然后创建您的形式更换主题皮肤功能,增添您在更换主题皮肤功能所需的属性。

想想Drupal的论坛吸?在这里添加您同意:https://www.drupal.org/node/2641072

dawehner’s picture

嗨,
什么多个复选框代表逻辑?难道各个复选框设置一个不同的字段的值?(例如现场1:黑皮鞋(是/否),现场2:白色的衬衫(是/否))。或者,你有一个字段(E,G,鞋的颜色),并希望用户选择一个独特的价值?

一个复选框表示二进制值(例如,开/关,1/0,真/假)。单选按钮允许人们选择一个独特的价值(如性别男或女)。一组复选框可以代表一对多值(例如检查您要订购的所有食品项目)。

总之,你有什么功能需求。贵的形式代表什么实体。什么是实体的字段(属性)?什么是你试图管理(请详细说明)基础数据对象?

dawehner’s picture

这其中往往是在我的眼中钉。

举例来说,我需要建立一个很好的铺出表,在左侧栏的复选框来指示任意元素的形式将采取行动。

有些表的行会被禁用。

还有的复选框元素来做到这一点没有什么好办法。事实上,你不能轻易地在所有禁用单个复选框。常重要的是要在视觉上显示给最终用户的禁用元件。但是如果你使用复选框元素,没有了大量的工作,你没有得到漂亮的单个返回数组,你会与复选框。这是负载的乐趣时,你有可能有它的元素任意数量的单一形式。

噢 - 我差点忘了 - 有复选框,当你渲染复选框元素,这一切都正确的,在一个地方。这使得它基本没用,如果你想格式化表,或者要渲染的复选框在多个点上任何时候。

如果有,让他们返回单个阵列,或单独呈现,并从复选框元素样式选项个人复选框元素链接一个简单的方法,那将是巨大的。

因此,没有人居然有这样做的什么好的建议?我还没有想出未加重实现一个优雅的解决方案。

dawehner’s picture

你可以做到这一切,如果你有你的表单主题功能。

在主题功能,您可以使用drupal_render从复选框元素渲染个别复选框,无论你想他们。你也可以做这样的事情改变一个人复选框的#TYPE从设置为隐藏所以在你的表中,可以挑选哪些复选框来显示,同时还具有自己的价值观通过的提交,这样你就不会失去前对于那些复选框 - 现有值。

我有做过没有太多的麻烦。你应该能够轻松地让他们禁用,而不是隐藏的,但我没有这样做(在我的经验Drupal有一个可怜的记录,其#disabled属性,所以它可能不只是工作,立竿见影)。

dawehner’s picture

一些快速笔记:

-即使您使用“单选小号 ”(或“复选框上课 '),你仍然可以定义子”单选“自定义属性的元素。comment_form_alter为例。这工作,因为form_process_radios不会覆盖现有的儿童。

-或者安装#after_build或#process处理程序。

- 有许多地方的Drupal布局复选框在表中(你会注意到在“斑马线”行)。它在主题化阶段完成。检查的代码。

- 对于禁用的表单元素:要小心。读#426056和#227966,并确保相关的错误确实固定在Drupal。尤其是确保已签禁用的按钮保持其状态。(另外:隐藏按钮,而不是禁用它们。)

来自  https://www.drupal.org/node/798744



 


FAPI: multiple 'checkbox' vs. 'checkboxes'

Hi,

I need to create a form with a couple of text fields and a few checkboxes. I can not use the 'checkboxes' element, as i need to add some custom attributes to each checkbox elements.

However, i keep running into problems when validation fails for one of the textfields. If one of the checkboxes are checked, then all checboxes appear as checked when drupal displays the validation errors. In other words, Drupal seems to be confused.

I've tried to compare how 'checkboxes' and multiple 'checkbox' elements are rendered, and there seems to be a difference. When using the 'checkbox' element, each checkbox gets its name value like "territory[1]". However, when using the 'checkbox' element, the name value is just "territory". I suspect this might be the reason why Drupal gets confused?

However, how can i get Drupal to understand that i want it to treat my 'checkbox' elements as 'checkboxes'?

Any ideas appreciated!

Comments

Jaypan’s picture

Use 'checkboxes', then create a themeing function for your form, adding the attributes you want in the themeing function.

Think the Drupal forums suck? Add your agreement here:https://www.drupal.org/node/2641072

dawehner’s picture

Hi,
What do the multiple check boxes represent logically? Does each individual check box set a value for a different field? (e.g. field 1: black shoes (yes/no), field 2: white shirt (yes/no)). Or, do you have one field (e,g, shoe color) and you want the user to select one exclusive value?

A check box represents a binary value (e.g. on/off, 1/0, true/false). Radio buttons allows folks to select one exclusive value (e.g. gender male or female). A set of check boxes can represent one to many values (e.g. check all the food items you want to order).

To summarize, what is your functional requirement. What entity does your form represent. What are the fields (attributes) of the entity? What is the underlying data object you are trying to manage (please detail)?

dawehner’s picture

This one has often been a thorn in my side.

For instance, I need to create a nicely-laid out table, with checkboxes in the left-hand column to indicate which arbitrary elements the form will act on.

Some table rows will be disabled.

There's no good way to do this with the checkboxes element. In fact, you can't easily disable individual checkboxes at all. Often it's important to visually show the disabled element to the end user. But if you use the checkbox element, without a lot of work, you don't get the nice single return array you would with checkboxes. This is loads of fun when you have a single form that could have an arbitrary number of elements in it.

Oh - and I almost forgot - with checkboxes, when you render the checkboxes element, it all goes right there in one spot. This makes it basically useless if you're trying to format a table, or any other time you want to render the checkboxes in more than one spot.

If there were a simple way to link individual checkbox elements so that they returned a single array, or to individually render and style options from a checkboxes element, that would be great.

So, does anyone actually have any good suggestions for doing this? I have yet to come up with an elegant solution that isn't aggravating to implement.

Coyote

dawehner’s picture

You can do all this if you have a theme function for your form.

In the theme function you can use drupal_render to render individual checkboxes from a checkboxes element wherever you want them. You can also do things like change the #type of an individual checkbox from the set to hidden so in your table you can pick and choose which checkboxes to display, while still having their values go through to the submit so you don't lose pre-existing values for those checkboxes.

I have done that before without much trouble. You should easily be able to make them disabled instead of hidden but I haven't done that (in my experience drupal has a poor track record with its #disabled attribute so it might not just work straight away).

dawehner’s picture

Some quick notes:

- Even if you use 'radios' (or 'checkboxes'), you can still define child 'radio' elements with custom attributes. See comment_form_alter for example. This works because form_process_radios doesn't overwrite existing children.

- Or install an #after_build or #process handler.

- There are many places where Drupal layouts checkboxes in a table (you'll note the "zebra" rows). It's done in the theming stage. Inspect the code.

- As for disabling form elements: be careful. Read #426056 and #227966 and make sure that a relevant bug was indeed fixed in Drupal. Especially make sure that disabled buttons that are checked retain their state. (Alternatively: hide the buttons, instead of disabling them.)

来自 https://www.drupal.org/node/798744


普通分类: