Last updated by admin 3 years ago
???(Validation)
??? ??? ?????
Grails??? ??? ???? ??(constraints)?? ???? ?? ???? ??? ??? ????? ???(validation)? ? ????. ??? Groovy ?? ??? ???? "constraints" ??? ??? ?? ?????:class User {
Long id
Long version String login
String password
String email
Date age def 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 } }
??? ??? ????
??? ??? ????? ???? ???(invalid), ??? ??? ??? ????? ??? ????. ??? ?? ??? ???? ?? ????? ?? ??? ????: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 }?? ??? ???
?? Grails? ?? ?? ???? ???? ??? ???? ?? ???, ??? ? ???? ??? ???? ??? ?? ????. ???? ???? "grails-app/i18n/messages.properties" ??? ?? ?? ??? ?? ???? ????? ???.????, ? ??? ?? ??? "user.login.length.tooshort" ????, ??? ?? ???? ???? ???:user.login.length.tooshort=???? ??? ?? ?? ????. ? ?? ??????.
Hibernate? ??? ???? ?? ??(constraints) ????
Grails? ?? ????? ???? ?? ?????? ????? ??? ????? ????? ??? ??? ?? ??(naming convention)? ??? "Constaints"? ??? ??? Groovy ????? ?????. ?? ?? ??? ??? "HibernateBook" ???? ?? "com/books/HibernateBookConstraints.groovy" ????? HibernateBook ???? ?? ???? ?????. ???? ???? GORM?? ? ? ?? constraints? ????? ???. ? ? ??? ?? @Property? ?? ????:constraints = {
title(length:5..15)
desc(blank:false)
}


