Grails Domain Verification-Constraints

Source: Internet
Author: User
Tags email account grails
Grails domain class verification (constraint attribute), that is, the 'straints' closure of the domain class, is used for validation of each field. grails provides a total of 16 Input Validation and a custom validation.

'Straints'

[Blank] (# gblank) ...... null

[Creditcard] (# creditcard) ...... credit card format

[Email] (# email) ...... email format

[Inlist] (# inlist) ...... specifies the range

[Matches] (# Matches) ...... Regular Expressions

[Max] (# Max) ...... maximum value (the type of the comparable Interface)

[Maxsize] (# maxsize) ...... maximum value of the Set

[Min] (# min) ...... minimum value (the type of the comparable Interface)

[Minsize] (# minsize) ...... minimum value of the Set

[Notequal] (# notequal) ...... exclude (cannot be the same as the set value)

[Nullable] (# nullable) ...... whether it is null

[Range] (# range )...

[Scale] (# scale) ...... Scale

[Size] (# size) ...... restrict the length of a set or string

[Unique] (# unique) ...... uniqueness

[Url] (# URL) ...... URL format

[Validator] (# validator) ...... custom Verification


##__ 1. Blank __
* _ FUNCTION __:
Define whether a field can be blank
* _ Usage __:

Class user {
Static constraints = {
Name (blank: false)
Email (blank: True)
Age (blank: false)
}
String name
String email
Integer age
}


##__ 2. creditcard __
* _ FUNCTION __:
Verify that the account is a valid credit card account
* _ Usage __:

Class user {
Static constraints = {
CardNumber (creditcard: True)
}
String cardNumber
}


##__ 3. Email __
* _ FUNCTION __:
Verify that the email account is valid
* _ Usage __:

Class user {
Static constraints = {
Email (Email: True)
}

String email
}


##__ 4. inlist __
* _ FUNCTION __:
Check whether it is an option in the specified array. The drop-down box is displayed on the page, and the option is the value specified in 'inlist '.
* _ Usage __:

Class user {
Static constraints = {
List (inlist: ['aaa', 'bbb ', 'ccc'])
}

String list
}


##__ 5. Matches __
* _ FUNCTION __:
Verify whether a string matches a regular expression.
* _ Usage __:

Class user {
Static constraints = {
Name (matches: "[A-Za-Z] + ")
}

String name
}


##__ 6. Max __
* _ FUNCTION __:
Set a class row that implements the 'java. Lang. comparable' interface as its own maximum value type. At the same time, this type must also be its own type.
* _ Usage __:

Class user {
Static constraints = {
Age (MAX: 100)
Date (MAX: new date ())
}

Integer age
Date
}


##__7. maxsize __
* _ FUNCTION __:
Sets the maximum value of a set or field.
* _ Usage __:

Class user {
Static constraints = {
People (maxsize: 100)
}

Integer people
}


##__ 8. min __
* _ FUNCTION __:
The function is opposite to 'Max'. The setting is the same as that of 'Max '.
* _ Usage __:

Class user {
Static constraints = {
Age (Min: 18)
Date (Min: new date ())
}

Integer age
Date
}


##__9. minsize __
* _ FUNCTION __:
Function is opposite to 'maxsize'. The setting is the same as 'maxsize '.
* _ Usage __:

Class user {
Static constraints = {
People (minsize: 10)
}

Integer people
}


##__ 10. notequal __
* _ FUNCTION __:
Verify that the entered user name cannot be the same as the specified user name
* _ Usage __:

Class user {
Static constraints = {
Name (notequal: "zhangsan ")
}

String name
}


##__ 11. nullable __
* _ FUNCTION __:
Verify whether the value can be 'null'. Note the difference between 'null' and 'null '.
* _ Usage __:

Class user {
Static constraints = {
Name (nullable: false)
}

String name
}


##__ 12. Range __
* _ FUNCTION __:
Set the range of a field
* _ Usage __:

Class user {
Static constraints = {
Age (range: 18 .. 100)
}

Integer age
}


##__13. Scale __
* _ FUNCTION __:
The constraint attribute that appears only when version 0.4 is released.
Based on the set scale value, the number of digits after the floating point is automatically adjusted to the set value.
Applicable to the following numeric types: 'java. Lang. float', 'java. Lang. double', 'java. Math. bigdecimal '(and its subclass ).
* _ Usage __:

Class user {
Static constraints = {
Price (scale: 2)
}

Double price
}


##__ 14. Size __
* _ FUNCTION __:
Specifies the length of a value, set, or string.
In version 0.5, we do not recommend that you use 'range' For numeric attributes '.
If 'blank 'is set to 'true' or 'nulllable' is set to 'true', this constraint attribute cannot be used.
* _ Usage __:

Class user {
Static constraints = {
Name (size: 5 .. 10)
}

String name
}


##__15. Unique __
* _ FUNCTION __:
Set whether the attribute must be unique
* _ Usage __:

Class user {
Static constraints = {
Name (unique: True)
}

String name
}


##__ 16. URL __
* _ FUNCTION __:
If the attribute is a URL address, set it to 'true '.
* _ Usage __:

Class user {
Static constraints = {
URL (URL: True)
}

String URL
}


##__ 17. validator __
* _ FUNCTION __:
Set custom verification in the closure
* _ Usage __:

Class user {
Static constraints = {
Repassword (validator: {Val, obj->
If (obj. Password! = Val ){
Return "repassword is not right"
}
})
}

String Password
String repassword
}

After 'constraints' is properly used, the generated scaffold is automatically verified.
Verify the information content. In the 'i18n' directory, you can find the corresponding language to change it. Do not forget to convert it to the corresponding encoding format using Java's 'native2ascii 'Encoding tool.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.