在Grails里,我們可以通過定義約束屬性來驗(yàn)證一個領(lǐng)域類的實(shí)例。約束屬性在一個叫“constraints”閉包的定義。可以為領(lǐng)域類里的每個屬性定義約束。
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。
同時,每個屬性的約束屬性都有與之對應(yīng)的錯誤消息(Error message code),當(dāng)表單未能通過驗(yàn)證的時候,將會返回這些錯誤消息。
這些錯誤消息在grails-app/i18n/message.properties里定義。
例如我們要讓User的email為空時返回"Please enter your email",則可以在message.properties定義:
user.email.blank=Please enter your email
如果用戶沒有自定義錯誤消息,系統(tǒng)則會用默認(rèn)的設(shè)置。當(dāng)然默認(rèn)的消息肯定不會是你想要的……
Grails提供很多驗(yàn)證屬性,可以滿足一些基本的驗(yàn)證需求:
blank
驗(yàn)證屬性能否為空,不允許為空則設(shè)為false。
Note: 如果在form里為空而提交,則屬性的值是一個空字符串,而不是null。
Example: login(blank:false)
Error message code: className.propertyName.blank
creditCard
如果要求屬性為信用卡號碼,則設(shè)為true。
Example: cardNumber(creditCard:true)
Error message code: className.propertyName.creditCard.invalid
email
如果要求屬性為emial地址,則設(shè)為true。
Example: contactEmail(email:true)
Error message code: className.propertyName.email.invalid
inList
如果要求屬性的值必須為規(guī)定的值,則定義規(guī)定的值。
Example: name(inList:["Joe", "Fred", "Bob"] )
Error message code: className.propertyName.not.inList
length
約束字符串或者數(shù)組的長度。
這個約束屬性在0.5版本是被取消,用size代替。
Example: login(length:5..15)
Error message code:
className.propertyName.length.toolong
className.propertyName.length.tooshort
matches
應(yīng)用正則表達(dá)式對字符串進(jìn)行驗(yàn)證。
Example: login(matches:"[a-zA-Z]+")
Error message code: className.propertyName.matches.invalid
max
設(shè)定屬性的最大值,值的類型必須跟屬性一樣。
Example:
age(max:new Date())
price(max:999F)
Error message code: className.propertyName.max.exceeded
maxLength
設(shè)定字符串或者數(shù)組的最大長度。
在0.5版本中被取消,由maxSize代替。
Example: login(maxLength:5)
Error message code: className.propertyName.maxLength.exceeded
maxSize
設(shè)定一個數(shù)字或者集合的最大大小。
在0.5版本中不被建議用在數(shù)字上,改用max。
Example: children(maxSize:25)
Error message code: className.propertyName.maxSize.exceeded
min
設(shè)定屬性的最小值。類型必須跟屬性一致。
Example:
age(min:new Date())
price(min:0F)
Error message code: className.propertyName.min.notmet
minLength
設(shè)定字符串屬性或者數(shù)組屬性的最小長度。
在0.5版本中被取消,由minSize代替。
Example: login(minLength:5)
Error message code: className.propertyName.minLength.notmet
minSize
設(shè)定一個數(shù)字或者集合的最小大小。
在0.5版本中不被建議用在數(shù)字屬性上,改用min。
Example: children(minSize:5)
Error message code: className.propertyName.minSize.notmet
notEqual
驗(yàn)證屬性的值是否跟指定的值相等。
Example: login(notEqual:"Bob")
Error message code: className.propertyName.notEqual
nullable
如果屬性不可以為null,則設(shè)為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才開始出現(xiàn)的約束屬性。
根據(jù)設(shè)定的scale數(shù)值,自動把浮點(diǎn)型數(shù)字小數(shù)點(diǎn)后的位數(shù)調(diào)整為設(shè)定的值。
適用于以下數(shù)值類型:java.lang.Float, Java.lang.Double, and Java.math.BigDecimal (and its subclasses)。
Example: salary(scale:2)
Error message code: 不返回錯誤信息
size
規(guī)定一個數(shù)值,集合或者字符串長度的大小。
在版本0.5中不被建議用在數(shù)字類型的屬性上,改用range。
Example: children(size:5..15)
Note: 不能使用這個約束屬性如果blank設(shè)為true或者nullable設(shè)為true。
Error message code:
className.propertyName.size.toosmall
className.propertyName.size.toobig
unique
如果屬性必須為唯一,則設(shè)為true。
Example: login(unique:true)
Note: 有可能會發(fā)生通過unique驗(yàn)證但是在隨后的數(shù)據(jù)庫儲存出現(xiàn)錯誤的情況。預(yù)防這種情況發(fā)生的方法是使用連續(xù)事務(wù)隔離級別或者進(jìn)行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地址,則設(shè)為true。
Example: homePage(url:true)
Error message code: className.propertyName.url.invalid
validator
在閉包里設(shè)定自定義的驗(yàn)證。
Example:
even( validator: {
return (it % 2) == 0
})
Error message code (default): className.propertyName.validator.invalid
將會在另外的文章里進(jìn)行介紹。
posted on 2008-07-21 22:47
周銳 閱讀(997)
評論(3) 編輯 收藏 所屬分類:
Groovy&Grails