<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 431,  comments - 344,  trackbacks - 0

    在Grails里,我們可以通過定義約束屬性來驗(yàn)證一個(gè)領(lǐng)域類的實(shí)例。約束屬性在一個(gè)叫“constraints”閉包的定義。可以為領(lǐng)域類里的每個(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)表單未能通過驗(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
    如果用戶沒有自定義錯(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ù)組的長度。
    這個(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è)定屬性的最大值,值的類型必須跟屬性一樣。
    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è)定一個(gè)數(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è)定一個(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才開始出現(xiàn)的約束屬性。
    根據(jù)設(shè)定的scale數(shù)值,自動(dòng)把浮點(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: 不返回錯(cuò)誤信息

    size
    規(guī)定一個(gè)數(shù)值,集合或者字符串長度的大小。
    在版本0.5中不被建議用在數(shù)字類型的屬性上,改用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ā)生通過unique驗(yàn)證但是在隨后的數(shù)據(jù)庫儲(chǔ)存出現(xiàn)錯(cuò)誤的情況。預(yù)防這種情況發(fā)生的方法是使用連續(xù)事務(wù)隔離級(jí)別或者進(jìn)行eception的處理。
    從版本0.5開始,unique的范圍(Scope)可以被指定。"Scope"是同一個(gè)類里其他屬性的名字,或者這些屬性名字的一個(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 周銳 閱讀(1009) 評(píng)論(3)  編輯  收藏 所屬分類: Groovy&Grails
    主站蜘蛛池模板: 久久嫩草影院免费看夜色| 免费观看一区二区三区| 色屁屁在线观看视频免费| 最近中文字幕大全免费版在线| 亚洲av日韩片在线观看| 亚洲视频一区网站| 九九视频高清视频免费观看| 日本最新免费网站| 国产亚洲av人片在线观看| 亚洲人成网站18禁止| 三年片在线观看免费观看大全动漫| 四虎国产精品永久免费网址| 亚洲成a人片在线观看无码| 国产亚洲视频在线观看| 1000部拍拍拍18免费网站| 亚洲伦理一二三四| 中文字幕不卡免费高清视频| 国产gav成人免费播放视频| 亚洲一区二区三区免费观看| 久久国产乱子精品免费女| 亚洲国产成人久久综合一区77| 亚洲av无码一区二区三区天堂古代| 91黑丝国产线观看免费| 亚洲无线电影官网| 色www永久免费网站| 亚洲精品尤物yw在线影院| 久久人午夜亚洲精品无码区| 亚洲免费综合色在线视频| 亚洲综合无码一区二区| 中文成人久久久久影院免费观看| 亚洲av无码专区国产乱码在线观看 | 亚洲乱妇熟女爽到高潮的片| 国产午夜鲁丝片AV无码免费| 中文字幕看片在线a免费| 亚洲女人影院想要爱| 日本免费大黄在线观看| 亚洲一区二区三区在线| 亚洲男人天堂2020| 亚洲黄色免费电影| 亚洲国产成人精品无码一区二区 | 午夜网站在线观看免费完整高清观看|