在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
周銳 閱讀(998)
評論(3) 編輯 收藏 所屬分類:
Groovy&Grails