我有一个表格,其中我必须对输入元素进行专门排序。所以我的表单看起来像这样:
<input type="text" name="name"/>
<select name="contacts.first">...</select>
<select name="contacts.second">...</select>
...
@Validatable
class MyCommand {
def name
def contacts
static constraints = { /* ... */ }
}
def update = { MyCommand cmd ->
if (cmd.validate()) {
/* ... */
}
}
cmd.contacts
时,它为null。如果我为每个名称都选择仅选择contacts
而不是contacts.first
,则它是期望的值数组,但是我不想依靠浏览器来确保这些项目按特定顺序排列。对这项工作有什么建议吗?正确的顺序至关重要。
最佳答案
最初的想法:http://stateyourbizness.blogspot.com/2009/02/binding-to-collection-fields-on-command.html
因此,对于您的命令对象,您可以使用:
import org.apache.commons.collections.FactoryUtils;
import org.apache.commons.collections.ListUtils;
class MyCommand {
def name
List contacts = ListUtils.lazyList([], FactoryUtils.constantFactory(''))
/* ... */
}
<input type="text" name="name"/>
<select name="contacts[0]">...</select>
<select name="contacts[1]">...</select>
https://stackoverflow.com/questions/7098183/