Yesod中动态调整的字段列表 [英] Dynamically-Sized List of Fields in Yesod
问题描述
在HTML中,可以使用非唯一名称指定多个字段,如下所示:
In HTML, multiple fields can be specified with a non-unique name like so:
<input type="checkbox" name="breakfast" value="eggs">
<input type="checkbox" name="breakfast" value="bacon">
,以便在提交时,查询参数将像breakfast=eggs&breakfast=bacon
一样传递(如果两个框都被打勾). CGI规范指出,这应该解释为数组或值列表,并且该技术对于动态调整输入列表的大小也很有用:
so that, when submitted, query parameters get passed like (if both boxes are ticked) breakfast=eggs&breakfast=bacon
. The CGI specification states that this should be interpreted as an array or list of values, and this technique is also useful for dynamically-sized lists of inputs:
<input type="text" name="url">
<input type="button" value="More…"
onclick="var s = document.createElement('input');
s.type='text';
s.name='url';
this.form.appendChild(s);
return false;">
但是,我看不到从Yesod中的表单中获取列表值输入的方法.有什么办法做这种事吗?
However, I can see no way to get list-valued inputs from a form in Yesod. Is there any way to do such a thing?
推荐答案
大多数预建字段在具有单个输入的输入上起作用(multiSelectField明显例外).为了实现所需的功能,您可能想创建一个自定义的
Most of the prebuilt fields work on inputs with a single input (with a notable exception for multiSelectField). To achieve what you're looking for, you probably want to create a custom Field. Notice that the fieldParse
function takes a list of Text
values, specifically to allow your use case.
表单章节包括有关自定义字段的部分.
The chapter on forms includes a section on custom fields.
这篇关于Yesod中动态调整的字段列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!