posts - 431,  comments - 344,  trackbacks - 0

          在Grails里,我們可以通過(guò)定義約束屬性來(lái)驗(yàn)證一個(gè)領(lǐng)域類(lèi)的實(shí)例。約束屬性在一個(gè)叫“constraints”閉包的定義。可以為領(lǐng)域類(lèi)里的每個(gè)屬性定義約束。

          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。

          同時(shí),每個(gè)屬性的約束屬性都有與之對(duì)應(yīng)的錯(cuò)誤消息(Error message code),當(dāng)表單未能通過(guò)驗(yàn)證的時(shí)候,將會(huì)返回這些錯(cuò)誤消息。
          這些錯(cuò)誤消息在grails-app/i18n/message.properties里定義。
          例如我們要讓User的email為空時(shí)返回"Please enter your email",則可以在message.properties定義:
          user.email.blank=Please enter your email
          如果用戶沒(méi)有自定義錯(cuò)誤消息,系統(tǒng)則會(huì)用默認(rèn)的設(shè)置。當(dāng)然默認(rèn)的消息肯定不會(huì)是你想要的……

          Grails提供很多驗(yàn)證屬性,可以滿足一些基本的驗(yàn)證需求:

          blank
          驗(yàn)證屬性能否為空,不允許為空則設(shè)為false。
          Note: 如果在form里為空而提交,則屬性的值是一個(gè)空字符串,而不是null。
          Example: login(blank:false)
          Error message code: className.propertyName.blank

          creditCard
          如果要求屬性為信用卡號(hào)碼,則設(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ù)組的長(zhǎng)度。
          這個(gè)約束屬性在0.5版本是被取消,用size代替。
          Example: login(length:5..15)
          Error message code:
          className.propertyName.length.toolong
          className.propertyName.length.tooshort

          matches
          應(yīng)用正則表達(dá)式對(duì)字符串進(jìn)行驗(yàn)證。
          Example: login(matches:"[a-zA-Z]+")
          Error message code: className.propertyName.matches.invalid

          max
          設(shè)定屬性的最大值,值的類(lèi)型必須跟屬性一樣。
          Example:
          age(max:new Date())
          price(max:999F)
          Error message code: className.propertyName.max.exceeded

          maxLength
          設(shè)定字符串或者數(shù)組的最大長(zhǎng)度。
          在0.5版本中被取消,由maxSize代替。
          Example: login(maxLength:5)
          Error message code: className.propertyName.maxLength.exceeded

          maxSize
          設(shè)定一個(gè)數(shù)字或者集合的最大大小。
          在0.5版本中不被建議用在數(shù)字上,改用max。
          Example: children(maxSize:25)
          Error message code: className.propertyName.maxSize.exceeded

          min
          設(shè)定屬性的最小值。類(lèi)型必須跟屬性一致。
          Example:
          age(min:new Date())
          price(min:0F)
          Error message code: className.propertyName.min.notmet

          minLength
          設(shè)定字符串屬性或者數(shù)組屬性的最小長(zhǎng)度。
          在0.5版本中被取消,由minSize代替。
          Example: login(minLength:5)
          Error message code: className.propertyName.minLength.notmet

          minSize
          設(shè)定一個(gè)數(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: 如果在表單里未填任何東西而提交時(shí),則作為request parameter,屬性的值為一個(gè)空字符串,而不是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才開(kāi)始出現(xiàn)的約束屬性。
          根據(jù)設(shè)定的scale數(shù)值,自動(dòng)把浮點(diǎn)型數(shù)字小數(shù)點(diǎn)后的位數(shù)調(diào)整為設(shè)定的值。
          適用于以下數(shù)值類(lèi)型:java.lang.Float, Java.lang.Double, and Java.math.BigDecimal (and its subclasses)。
          Example: salary(scale:2)
          Error message code: 不返回錯(cuò)誤信息

          size
          規(guī)定一個(gè)數(shù)值,集合或者字符串長(zhǎng)度的大小。
          在版本0.5中不被建議用在數(shù)字類(lèi)型的屬性上,改用range。
          Example: children(size:5..15)
          Note: 不能使用這個(gè)約束屬性如果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: 有可能會(huì)發(fā)生通過(guò)unique驗(yàn)證但是在隨后的數(shù)據(jù)庫(kù)儲(chǔ)存出現(xiàn)錯(cuò)誤的情況。預(yù)防這種情況發(fā)生的方法是使用連續(xù)事務(wù)隔離級(jí)別或者進(jìn)行eception的處理。
          從版本0.5開(kāi)始,unique的范圍(Scope)可以被指定。"Scope"是同一個(gè)類(lèi)里其他屬性的名字,或者這些屬性名字的一個(gè)list。
          Example: group(unique:'department')
          上面的例子里group名在一個(gè)department里是唯一的,但是可能在其他department里有相同名字的groups。
          Another Example: login(unique:['group','department'])
          在這個(gè)例子,login在group和department里必須是唯一的。可能在不同等group和department里會(huì)有相同的login。
          Error message code: className.propertyName.unique

          url
          如果屬性為一個(gè)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
          將會(huì)在另外的文章里進(jìn)行介紹。

          posted on 2008-07-21 22:47 周銳 閱讀(1006) 評(píng)論(3)  編輯  收藏 所屬分類(lèi): Groovy&Grails
          主站蜘蛛池模板: 偏关县| 舟曲县| 托克逊县| 顺义区| 嘉义市| 虹口区| 东辽县| 巴中市| 兴业县| 焉耆| 湄潭县| 荣昌县| 泉州市| 上林县| 福泉市| 澄江县| 察雅县| 资阳市| 安仁县| 南川市| 咸丰县| 太和县| 平江县| 金华市| 化德县| 涿州市| 洛川县| 梁河县| 五大连池市| 略阳县| 江永县| 滦平县| 德安县| 邯郸县| 洞口县| 永昌县| 互助| 高要市| 洱源县| 盘锦市| 胶南市|