Search code examples
validationgrailsgsp

Grails verify email re-enter


I have a domain class for user registration. During the registration, user needs to enter email twice, and those two needs to match. I do not want to store re-entered email and just want to use that for verification. Any suggestion?

My domain class looks like:

class MyUser{

    String name
    String email
    Integer telephone
 }

and my view looks like:

<label for="reEmail" id="reEmail">
<g:message code="myUser.reEnter.label" default="Re-enter Email:" id="reEnter"/></label>
<g:textField name="reEmail"/>

Thanks in advance


Solution

  • A good way to handle this is to use a transient field in your class.

    Like so....

    class MyUser{
    
        static constraints = {
          email (email: true, blank: false) //<-- just a good idea ;)
        }
    
        String name
        String email
        Integer telephone
        String emailAgain
        static transients = ['emailAgain'] //<-- won't be stored in the database
    
     }
    

    Then, in you view you can deal with the field just like any other...

    <g:textField name="email" />
    <g:textField name="emailAgain" />
    

    Then just validate it in your controller...

    def save = {
       def myUser = new MyUser(params);
       if (myUser.email.equals(myUser.emailAgain)){ //<-- maybe make a helper method in the domain class (myUser.doesEmailMatchEmailAgain() ?)
         // do stuff
       }else{
         //handle error, tell user their email's don't match
       }
    }