Thursday, 20 September 2012

Grails messages not working in the gsp views

So I was having this annoying issue whenever there was a validation error on one of my forms. I have configured the message key correctly and I know the feature works because I used it extensively before.

Error Field error in object 'com.privategram.User' on field 'email': rejected value [tommytcchan@gmail.com]; codes [com.privategram.User.email.unique.error.com.privategram.User.email,com.privategram.User.email.unique.error.email,com.privategram.User.email.unique.error.java.lang.String,com.privategram.User.email.unique.error,user.email.unique.error.com.privategram.User.email,user.email.unique.error.email,user.email.unique.error.java.lang.String,user.email.unique.error,com.privategram.User.email.unique.com.privategram.User.email,com.privategram.User.email.unique.email,com.privategram.User.email.unique.java.lang.String,com.privategram.User.email.unique,user.email.unique.com.privategram.User.email,user.email.unique.email,user.email.unique.java.lang.String,user.email.unique,unique.com.privategram.User.email,unique.email,unique.java.lang.String,unique]; arguments [email,class com.privategram.User,tommytcchan@gmail.com]; default message [Property [{0}] of class [{1}] with value [{2}] must be unique]

This was what I had originally.


 <g:haserrors bean="${userInstance}">  
 </g:haserrors>  
 <br />  
 <ul class="errors" role="alert">  
      <g:eacherror bean="${userInstance}" var="error">  
           <div class="alert alert-error">  
                <button class="close" data-dismiss="alert" type="button">×</button>  
                <strong>Error</strong>  
                ${error}  
           </div>  
      </g:eacherror>  
 </ul>  



...it seemed fair to me, which made me think it could be because I upgraded the version of Grails for this current project. However, upon comparing this to my other project, I realized it was an user (*cough* `me`) error. You have to wrap the error param in a g:message tag!


 <g:haserrors bean="${userInstance}">  
      <ul class="errors" role="alert">  
           <g:eacherror bean="${userInstance}" var="error">  
                <div class="alert alert-error">  
                     <button class="close" data-dismiss="alert" type="button">×</button>  
                     <strong>Error</strong>  
                     <g:message error="${error}">  
                     </g:message>  
                </div>  
           </g:eacherror>  
      </ul>  
 </g:haserrors>  


No comments:

Post a Comment