Last updated by admin 4 years ago
??????? {excerpt:hidden=true}Validation{excerpt}
??????????????????? {excerpt:hidden=true}Validating Domain Classes{excerpt}
{excerpt:hidden=true} Grails allows you to apply constraints to a domain class that can then be used to validate a domain class instance. Constraints are applied using a "constraints" closure which uses the Groovy builder syntax to configure constraints against each property name, for example:{excerpt} Grails??????????????????????????????????????????????constraints(????)??????????????????????????constraints?????????Groovy?????????????? "constraints"????????????????class User {
Long id
Long version 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)
}
}def user = new User() // populate properties if(user.validate()) { // do something with user } else { user.errors.allErrors.each { println it } }
if(user.save()) { return user } else { user.errors.allErrors.each { println it } }
??????????? {excerpt:hidden=true}Display Errors in the View{excerpt}
{excerpt:hidden=true} So your instance doesn't validate, how do you now display an appropriate error message in the view? For starters you need to redirect to the right action or view with your erroneous bean:{excerpt} ?????????????????????????????????????????????????????????????? ?????????????????????????????bean??????????????????:class UserController {
def save = {
def u = new User()
u.properties = params
if(u.save()) {
// do something
}
else {
render(view:'create',model:[user:u])
}
}
}chain(action:create,model:[user:u])
<g:hasErrors bean="${user}> <g:renderErrors bean="${user}" as="list" /> </g:hasErrors>
<div class="prop ${hasErrors(bean:user,field:'login', 'errors')}"> <label for="login"><input type="text" name="login" /> </div>
.errors { border: 1px solid red }??????????? {excerpt:hidden=true}Changing the Error Message{excerpt}
{excerpt:hidden=true} Of course the default error message that Grails displays is probably not what you were after, so you will want to change this. The way you do this is by modifying the "grails-app/i18n/messages.properties" file and adding a message for the particular error code.{excerpt} ????Grails????????????????????????????????????????????????????????????????????"grails-app/i18n/messages.properties"???????????????????????????????????{excerpt:hidden=true} For example if we follow the above example the error code may be "user.login.length.tooshort" so we add an entry:{excerpt} ???????????????"user.login.length.tooshort"??????????????????????:user.login.length.tooshort=I'm sorry the login you entered wasn't quite long enough, please make it longerHibernate????????????constraints??? {excerpt:hidden=true}Defining constraints for Hibernate mapped classes{excerpt}
{excerpt:hidden=true} To integrate with the Grails constraints mechanism and hence hook into useful things like the way the views are generated and Grails' Validationmechanism you can do so by creating a Groovy script following the naming convention of your domain class and ending in the "Constraints" suffix. For example for a "com.books.HibernateBook" class (either an EJB3 entity of mapped with Hibernate XML) defined above you would need to create a "com/books/HibernateBookConstraints.groovy" script in the same package as the class itself, in the src/java directory tree. Within the script just define constraints in the same way as you would do in a GORM class but without the @Property notation:{excerpt} Hibernate??????????Grails constraints??????Grails ?????????????????????????????????"Constraints"?????????????????????Groovy????????????????"com.books.HibernateBook"???(????Hibernate XML?????????EJB entity)??????????????????????????(src/java ????????) "com/books/HibernateBookConstraints.groovy"???????????????????????????? ??GORM?????????constraints???????????@Property?????????????:constraints = {
title(length:5..15)
desc(blank:false)
}


