posts - 431,  comments - 344,  trackbacks - 0

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

class User {
String login
String password
String email
Date age

static constraints = {
login(length:5..15,blank:false,unique:true)
password(length:5..15,blank:false)
email(email:true,blank:false)
age(min:new Date(),nullable:false)
}
}

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
将会在另外的文章里进行介绍。

posted on 2008-07-21 22:47 周锐 阅读(973) 评论(3)  编辑  收藏 所属分类: Groovy&Grails

只有注册用户登录后才能发表评论。


网站导航: