Japanese Validation Reference

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} In the reference that follows, className.propertyName.validationConstraint refers to the error message codes defined in _grails-app/i18n/message.properties_. For example, referring to the above class, the error message might be defined as: _user.login.blank=Please enter a login_{excerpt} ????????????????????? className.propertyName.validationConstraint ??_grails-app/i18n/message.properties_ ?????????????????? ???????????? _user.login.blank=Please enter a login_

????????????????? {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)
???????????: className.propertyName.blank ????.???????.xxxxxxx (????)

creditcard

????: ???????????????????????????true???? {excerpt:hidden=true}set to truee if a string should be a credit card number {excerpt} ?:

cardNumber(creditcard:true)
???????????: className.propertyName.?????

email

????: ????????????????true???? {excerpt:hidden=true}set to true if a string value is an email address {excerpt} ?:

contactEmail(email:true)
???????????: className.propertyName.email.invalid

inList

????: ??????????? {excerpt:hidden=true}constrains a value so that it must be contained within the given list {excerpt} ?:

name(inList:["Joe", "Fred", "Bob"] )
???????????: className.propertyName.not.inList

length

????: Groovy? range ?????????????? {excerpt:hidden=true}Uses a Groovy range to restrict the length of a string or array {excerpt} ?:

login(length:5..15)
???????????: className.propertyName.length.toolong, className.propertyName.length.tooshort

min

????: java.lang.Comparable?????????????????? {excerpt:hidden=true}sets the minimum value of a class that implements java.lang.Comparable {excerpt} ?:

age(min:new Date())
???????????: className.propertyName.min.notmet

minLength

????: ??????????????? {excerpt:hidden=true}sets the minimum length of a string or array property {excerpt} ?:

login(minLength:5)
???????????: className.propertyName.minLength.notmet

minSize

????: ?????????????????? {excerpt:hidden=true}sets the minimum size of a collection or number property {excerpt} ?:

children(minSize:5)
???????????: className.propertyName.minSize.notmet

matches

????: ????????????????? {excerpt:hidden=true}Applies a regular expression against a string value {excerpt} ?:

login(matches:"[a-zA-Z]")
???????????: className.propertyName.matches.invalid

max

????: java.lang.Comparable?????????????????? {excerpt:hidden=true}sets the maximum value of a class that implements java.lang.Comparable {excerpt} ?:

age(max:new Date())
???????????: className.propertyName.max.exceeded

maxLength

????: ??????????????? {excerpt:hidden=true}sets the maximum length of a string or array property {excerpt} ?:

login(maxLength:5)
???????????: className.propertyName.maxLength.exceeded

maxSize

????: ?????????????????? {excerpt:hidden=true}sets the maximum size of a collection or number property {excerpt} ?:

children(maxSize:25)
???????????: className.propertyName.maxSize.exceeded

notEqual

????: ????????????????? {excerpt:hidden=true}validates that a property is not equal to the specified value {excerpt} ?:

login(notEqual:"Bob")
???????????: className.propertyName.notEqual

nullable

????: null??????????false???? {excerpt:hidden=true}set to false if the property value cannot be null {excerpt} ?:

age(nullable:false)
???????????: className.propertyName.nullable

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)
???????????: className.propertyName.range.toosmall or className.propertyName.range.toobig

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)
Error message code: N/A

size

????: Groovy? range ????????????????????? {excerpt:hidden=true}Uses a Groovy range to restrict the size of a collection or number {excerpt} ?:

children(size:5..15)
???????????: className.propertyName.size.toosmall or className.propertyName.size.toobig

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)
???????????: className.propertyName.unique

url

????: ????URL?????????true???? set to true if a string value is a URL address ?:

homePage(url:true)
???????????: className.propertyName.url.invalid

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'] }) } }

??????????? (?????): className.propertyName.validator.invalid