Korean Validation

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

??? ??? ????? ?????? "validate" ???? ???? ???:
def user =  new User()
       // populate properties

if(user.validate()) { // do something with user } else { user.errors.allErrors.each { println it } }

??? ???? {{errors}} ??? Spring ?????? org.springframework.validation.Errors ?????? ???? ???????.

????? "save" ???? ???? DB? ???? ?? ???? ???? ?????. ??? ??? ??? ?? ??? ? ????:

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])
           }
    }
}
? ????? ??? ?? ???? ?? render ???? ?????, ??? ?? ???(chaining)? ?? "create" ???? ??? ?? ????:
chain(action:create,model:[user:u])
chain ???? ??? ??? ???(flash scope)? ?????. ??? HTTP ??? ????? ????? ??? ? ?? ???.

???? ?? ???????. ??? ?? ??? ??? ????? ???? ????, "hasErrors" ?? ??? ??? ???? ? ???? ??? ? ????:

<g:hasErrors bean="${user}>
         <g:renderErrors bean="${user}" as="list" />
   </g:hasErrors>
? ????? ??? ???? ???? ?? hasErrors ??? "renderErrors" ??? ?? ??? ????. GSP??? ??? ?? ????? ??? ? ?? ??? ??? ? ?? ??? ????? ?? ??? ?? ??? ? ?? ????:
<div class="prop ${hasErrors(bean:user,field:'login', 'errors')}">
    <label for="login"><input type="text" name="login" />
</div>
? ??? ???? 'login' ??? ??? ??? ?? ?? ??? "errors"?? CSS class? ???? ????. CSS style? ??? ?? ??? ? ????:
.errors { border: 1px solid red }
??? ??, ??? ???? ?? ???? ??? UI? ?? ? ????.

?? ??? ???

?? Grails? ?? ?? ???? ???? ??? ???? ?? ???, ??? ? ???? ??? ???? ??? ?? ????. ???? ???? "grails-app/i18n/messages.properties" ??? ?? ?? ??? ?? ???? ????? ???.

????, ? ??? ?? ??? "user.login.length.tooshort" ????, ??? ?? ???? ???? ???:

user.login.length.tooshort=???? ??? ?? ?? ????. ? ?? ??????.
??? ?? ?? ?? ? ??? ???? ??? ????(validation constraints)?? ???? ??? ???? ? ?????.

Hibernate? ??? ???? ?? ??(constraints) ????

Grails? ?? ????? ???? ?? ?????? ????? ??? ????? ????? ??? ??? ?? ??(naming convention)? ??? "Constaints"? ??? ??? Groovy ????? ?????. ?? ?? ??? ??? "HibernateBook" ???? ?? "com/books/HibernateBookConstraints.groovy" ????? HibernateBook ???? ?? ???? ?????. ???? ???? GORM?? ? ? ?? constraints? ????? ???. ? ? ??? ?? @Property? ?? ????:
constraints = {
      title(length:5..15)
      desc(blank:false)
}