2011-08-17 56 views
1

我有一个表单,我必须具体订购输入元素。所以我的表单看起来像这样:Grails命令对象和输入名称值

<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,它为空。如果我将每个名称命名为contacts而不是contacts.first,则这是一个预期值的数组,但我不想依赖浏览器来确保这些项目按特定顺序排列。任何建议,使这项工作?正确的顺序至关重要。

回答

2

的原始灵感: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('')) 
    /* ... */ 
} 

,有你的HTML看起来像:

<input type="text" name="name"/> 
<select name="contacts[0]">...</select> 
<select name="contacts[1]">...</select>