grails 命令对象和带有前缀的字段

发布于 2024-12-23 12:44:02 字数 614 浏览 1 评论 0原文



我使用 grails 1.3.7,情况如下...
有一个巨大的表单,其字段有几个不同的前缀(稍后用于数据绑定),并尝试通过命令对象进行验证...但是,在前缀给我带来了困难,无法在命令对象中正确映射名称...请问有什么建议吗?

表单中具有类似这样的字段:

<input name="dealer.name" value="${dealer.name}" type="text"> 

对于命令对象:

class somethingCommand {
    String name
    Map dealer = [:]
    static constraints = {
        dealer validator: {
            val, obj ->
            obj.properties["name"] != ""
        }
    }
}

如果......我们会怎样?以另一种方式查看它并在传递给命令之前映射参数对象...我应该如何在不使用 grails 魔法的情况下将参数传递给命令对象?!?!?!

tnx

Im using grails 1.3.7 and here is the case...
Have a huge form with a few different prefixes for its fields (later used in data binding) and trying to validate thru a command object... however the lovely DOT used in prefixes is giving me a hard time and cannot get the names mapped properly in command object... any suggestion please?

in the form have fields like field like this one:

<input name="dealer.name" value="${dealer.name}" type="text"> 

and for command object:

class somethingCommand {
    String name
    Map dealer = [:]
    static constraints = {
        dealer validator: {
            val, obj ->
            obj.properties["name"] != ""
        }
    }
}

what if.... we look at it in another way and map the parameters before passing to command object... how should I pass my parameters to a command object without using grails magic?!?!?!

tnx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

山色无中 2024-12-30 12:44:02

您可以通过在控制器中获取“经销商”地图

def dealerMap = params["dealer"]

,然后手动创建经销商命令对象并将地图内容绑定到它。

def dealerCommand = new DealerCommand() 
bindData(dealerCommand , dealerMap)

然后您可以正常使用命令对象的验证

you could grab the "dealer" map in the controller via

def dealerMap = params["dealer"]

and then create a dealer command opject by hand and bind the map content to it.

def dealerCommand = new DealerCommand() 
bindData(dealerCommand , dealerMap)

you can then use the validation of the command object as normal

深海夜未眠 2024-12-30 12:44:02

支持带有命令对象前缀的数据绑定属性:

对于命令:

class DealerCommand {
    String name
    Map dealer = [:]
}

名为“name”、“dealer”、“dealer.name”和“dealer.dealer”的属性将正确绑定到命令对象。

http://grails.org/doc/2.3.x/guide/single .html#commandObjects

Databinding properties with prefixes to command objects is supported:

For the command:

class DealerCommand {
    String name
    Map dealer = [:]
}

Properties named 'name', 'dealer', 'dealer.name' and 'dealer.dealer' will be bound correctly to the command object.

http://grails.org/doc/2.3.x/guide/single.html#commandObjects

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文