Last updated by admin 4 years ago
??????? ??????? {excerpt:hidden=true}Domain Class Validation {excerpt}
? {excerpt:hidden=true}Example {excerpt}
class User {
Long id
Long version String login
String password
static constraints = {
login(length:5..15,blank:false,unique:true)
password(length:5..15,blank:false)
}
}????????????????? {excerpt:hidden=true}Validation Constraint Reference {excerpt}
blank
????: ?????????????false???? {excerpt:hidden=true} set to false if a string value cannot be blank {excerpt} ?:login(blank:false)creditcard
????: ???????????????????????????true???? {excerpt:hidden=true}set to truee if a string should be a credit card number {excerpt} ?:cardNumber(creditcard:true)contactEmail(email:true)inList
????: ??????????? {excerpt:hidden=true}constrains a value so that it must be contained within the given list {excerpt} ?:name(inList:["Joe", "Fred", "Bob"] )
length
????: Groovy? range ?????????????? {excerpt:hidden=true}Uses a Groovy range to restrict the length of a string or array {excerpt} ?:login(length:5..15)
min
????: java.lang.Comparable?????????????????? {excerpt:hidden=true}sets the minimum value of a class that implements java.lang.Comparable {excerpt} ?:age(min:new Date())minLength
????: ??????????????? {excerpt:hidden=true}sets the minimum length of a string or array property {excerpt} ?:login(minLength:5)
minSize
????: ?????????????????? {excerpt:hidden=true}sets the minimum size of a collection or number property {excerpt} ?:children(minSize:5)
matches
????: ????????????????? {excerpt:hidden=true}Applies a regular expression against a string value {excerpt} ?:login(matches:"[a-zA-Z]")max
????: java.lang.Comparable?????????????????? {excerpt:hidden=true}sets the maximum value of a class that implements java.lang.Comparable {excerpt} ?:age(max:new Date())maxLength
????: ??????????????? {excerpt:hidden=true}sets the maximum length of a string or array property {excerpt} ?:login(maxLength:5)
maxSize
????: ?????????????????? {excerpt:hidden=true}sets the maximum size of a collection or number property {excerpt} ?:children(maxSize:25)
notEqual
????: ????????????????? {excerpt:hidden=true}validates that a property is not equal to the specified value {excerpt} ?:login(notEqual:"Bob")nullable
????: null??????????false???? {excerpt:hidden=true}set to false if the property value cannot be null {excerpt} ?:age(nullable:false)range
????: Groovy? range ??????????????????????? {excerpt:hidden=true}Uses a Groovy range to ensure that a property's value occurs within a specified range {excerpt} ?:age(range:minAge..maxAge)
scale (Since 0.4)
Usage: Set to the desired scale for floating point numbers (i.e., the number of digits to the right of the decimal point). This constraint is applicable for properties of the following types: java.lang.Float, java.lang.Double, and java.math.BigDecimal (and its subclasses). When validation is invoked, this constraint determines if the number includes more nonzero decimal places than the scale permits. If so, it automatically rounds the number to the maximum number of decimal places allowed by the scale. This constraint does not generate validation error messages. Example:salary(scale:2)
size
????: Groovy? range ????????????????????? {excerpt:hidden=true}Uses a Groovy range to restrict the size of a collection or number {excerpt} ?:children(size:5..15)
unique
????: true????????????????????????????????????????????????? {excerpt:hidden=true}set to true if the property must be unique (this is a persistent call and will query the database) {excerpt} ?:login(unique:true)url
????: ????URL?????????true???? set to true if a string value is a URL address ?:homePage(url:true)validator
????: ????????????????????????????????????????????????????????????????????????????????????????????? {excerpt:hidden=true}set to a Closure to use custom validation. A single or no parameter Closure receives the value, a two-parameter Closure receives the value and object reference {excerpt} {excerpt:hidden=true}The closure can return:- null or true to indicate that the value is valid
- false to indicate an invalid value and use the default message code
- a string to indicate the error code to append to the "classname.propertName." string to display an error
- a list containing a string as above, and then any number of arguments following it, which can be used as formatted message arguments indexed at 3 onwards. See grails-app/i18n/message.properties to see how the default error message codes use the arguments.{excerpt}
- ?????????null??true
- ????????????????????false
- ?????????????"classname.propertName."?????????????????
- ??????????????????????????????????????????????????????????????????????? grails-app/i18n/message.properties ????
even( validator: {
return (it % 2) == 0
})
password1( validator: {
val, obj ->
obj.properties['password2'] == val
})
magicNumber( validator:
someClosureWithTwoParameters)
// ????????????????????????:
// classname.propertyName.custom.error=My error shows arguments {3} and {4} for value {0}
otherProperty( validator: { return ['custom.error', arg1, arg2] } )
// The following example does not use custom validation.
// A custom message may be defined in messages.properties:
// user.login.blank=Please enter a login
// which will be used instead of default.blank.message
class User {
String login
static constraints = {
login(blank:false)
}
}// In the following example, custom validation is used:
// user.login.validator.invalid=Please enter a login
class User {
String login
static constraints = {
login(validator: {
return (it.length != 0)
})
}
}// The following might define the error message as:
// user.login.invalid.bountyhunter=Invalid bounty hunter ({2}) tried to log in. (Class name = {1}. Property name = {0})
class User {
String login
static constraints = {
login(validator: {
if (!it.startsWith('boba')) return ['invalid.bountyhunter']
})
}
}


