Grails命令对象临时字段验证 [英] Grails command object temporary field validation

查看:133
本文介绍了Grails命令对象临时字段验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用命令对象验证字段的用户注册表单。其中一个字段是复选框,它在继续注册之前必须进行检查,并且不会保存到域对象。该复选框在命令对象中具有相应的布尔字段。如果未选中复选框,则会从自定义验证器中引发验证错误。

问题是,这个错误不会在< g:renderErrors bean =$ {command}as =xml /> 块(验证器被正确触发)。



命令对象:

  class RegisterCommand {

...
布尔termsChecked
...
静态约束= {
...
termsChecked验证器:RegisterController.termsCheckedValidator
}

Validator:

  static final termsCheckedValidator = {termsChecked,command,errors  - > 
if(!command.termsChecked){
return'registerCommand.termsChecked.required'
}
}


GSP文件中的复选框: $ {command.termsChecked}bean =$ {command}name ='termsChecked'/>

如何解决这个问题?

解决方案

如果您将3个参数传递给验证器,并且最后一个参数为 errors ,那么返回代码将被忽略,因为认为Spring错误正在进行如果您想使用错误代码,那么只需将2参数传递给验证器即可。

  static final termsCheckedValidator = {termsChecked,command  - > 
if(!command.termsChecked){
return ['required.termsChecked']
}
}


//消息。属性
registerCommand.termsChecked.required.termsChecked = blah


I have a user registration form where the fields are validated using command object. One of the fields is a checkbox, which must checked before proceeding the registration, and it isn't saved to the domain object. This checkbox has a corresponding Boolean field in the command object. When checkbox is not checked, a validation error is thrown from a custom validator.

The problem is, that this error is not propagated in the <g:renderErrors bean="${command}" as="xml"/> block (the validator is fired correctly).

The command object:

class RegisterCommand {

...
Boolean termsChecked
...
static constraints = {
    ...
    termsChecked validator: RegisterController.termsCheckedValidator
}

Validator:

static final termsCheckedValidator = {termsChecked, command, errors ->
    if (!command.termsChecked) {
        return 'registerCommand.termsChecked.required'
    }
}

Checkbox in the GSP file:

<g:checkBox value="${command.termsChecked}" bean="${command}" name='termsChecked'/>

How this could be solved?

解决方案

If you pass 3 parameters to the validator with the last one being errors then the return code is ignored thinking that the Spring Errors is taking care of errors.

If you want to use the error code then just pass 2 parameter to the validator as

static final termsCheckedValidator = {termsChecked, command ->
    if (!command.termsChecked) {
        return ['required.termsChecked']
    }
}


//messages.properties
registerCommand.termsChecked.required.termsChecked=blah

这篇关于Grails命令对象临时字段验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆