Grails的域类验证详解

本文参考:http://hi.baidu.com/javacookies/blog/item/18fc2a3d30181ccf9e3d62b1.html

在Grails里,可以通过定义约束属性来验证一个领域类的实例。约束属性在一个叫"constraints"闭包的定义。可以为领域类里的每个属性定义约束。

class TUser {
    String name
    String password
    String demo
    String help
   
    String toString(){
        "$name:$password"
    }
   
    static constraints = {
        name(blank: false)
        password(blank: false,size: 6 .. 16)
        demo(blank: true,nullable: true)
        help(blank: true,nullable: true)
    }
}


constraints 必须声明为static。

同时,每个属性的约束属性都有与之对应的错误消息(Error message code),当表单未能通过验证的时候,将会返回这些错误消息。
这些错误消息在grails-app/i18n/message.properties里定义。
例如我们要让User的email为空时返回"Please enter your email",则可以在message.properties定义:
user.email.blank=Please enter your email
如果用户没有自定义错误消息,系统则会用默认的设置。当然默认的消息肯定不会是你想要的......

Grails提供很多验证属性,可以满足一些基本的验证需求:

blank
验证属性能否为空,不允许为空则设为false。
Note: 如果在form里为空而提交,则属性的值是一个空字符串,而不是null。
Example: login(blank:false)
Error message code: className.propertyName.blank

creditCard
如果要求属性为信用卡号码,则设为true。
Example: cardNumber(creditCard:true)
Error message code: className.propertyName.creditCard.invalid

email
如果要求属性为emial地址,则设为true。
Example: contactEmail(email:true)
Error message code: className.propertyName.email.invalid

inList
如果要求属性的值必须为规定的值,则定义规定的值。
Example: name(inList:"Joe", "Fred", "Bob" )
Error message code: className.propertyName.not.inList

length
约束字符串或者数组的长度。
这个约束属性在0.5版本是被取消,用size代替。
Example: login(length:5..15)
Error message code:
className.propertyName.length.toolong
className.propertyName.length.tooshort

matches
应用正则表达式对字符串进行验证。
Example: login(matches:"a-zA-Z +")
Error message code: className.propertyName.matches.invalid

max
设定属性的最大值,值的类型必须跟属性一样。
Example:
age(max:new Date())
price(max:999F)
Error message code: className.propertyName.max.exceeded

maxLength
设定字符串或者数组的最大长度。
在0.5版本中被取消,由maxSize代替。
Example: login(maxLength:5)
Error message code: className.propertyName.maxLength.exceeded

maxSize
设定一个数字或者集合的最大大小。
在0.5版本中不被建议用在数字上,改用max。
Example: children(maxSize:25)
Error message code: className.propertyName.maxSize.exceeded

min
设定属性的最小值。类型必须跟属性一致。
Example:
age(min:new Date())
price(min:0F)
Error message code: className.propertyName.min.notmet

minLength
设定字符串属性或者数组属性的最小长度。
在0.5版本中被取消,由minSize代替。
Example: login(minLength:5)
Error message code: className.propertyName.minLength.notmet

minSize
设定一个数字或者集合的最小大小。
在0.5版本中不被建议用在数字属性上,改用min。
Example: children(minSize:5)
Error message code: className.propertyName.minSize.notmet

notEqual
验证属性的值是否跟指定的值相等。
Example: login(notEqual:"Bob")
Error message code: className.propertyName.notEqual

nullable
如果属性不可以为null,则设为false。
Note: 如果在表单里未填任何东西而提交时,则作为request parameter,属性的值为一个空字符串,而不是null。
Example: age(nullable:false)
Error message code: className.propertyName.nullable

range
限制属性的值在指定的范围里。
Example: age(range:minAge..maxAge)
Error message code:
className.propertyName.range.toosmall
className.propertyName.range.toobig

scale
版本0.4才开始出现的约束属性。
根据设定的scale数值,自动把浮点型数字小数点后的位数调整为设定的值。
适用于以下数值类型:java.lang.Float, Java.lang.Double, and Java.math.BigDecimal (and its subclasses)。
Example: salary(scale:2)
Error message code: 不返回错误信息

size
规定一个数值,集合或者字符串长度的大小。
在版本0.5中不被建议用在数字类型的属性上,改用range。
Example: children(size:5..15)
Note: 不能使用这个约束属性如果blank设为true或者nullable设为true。
Error message code:
className.propertyName.size.toosmall
className.propertyName.size.toobig

unique
如果属性必须为唯一,则设为true。
Example: login(unique:true)
Note: 有可能会发生通过unique验证但是在随后的数据库储存出现错误的情况。预防这种情况发生的方法是使用连续事务隔离级别或者进行eception的处理。
从版本0.5开始,unique的范围(Scope)可以被指定。"Scope"是同一个类里其他属性的名字,或者这些属性名字的一个list。
Example: group(unique:'department')
上面的例子里group名在一个department里是唯一的,但是可能在其他department里有相同名字的groups。
Another Example: login(unique:'group','department' )
在这个例子,login在group和department里必须是唯一的。可能在不同等group和department里会有相同的login。
Error message code: className.propertyName.unique

url
如果属性为一个URL地址,则设为true。
Example: homePage(url:true)
Error message code: className.propertyName.url.invalid

validator
在闭包里设定自定义的验证。
Example:

even( validator:
{ return

 (it % 2) == 0 }
)
Error message code (default

): className.propertyName.validator.invalid
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值