title: Unable to resolve Grails custom validator issue
body: <p>I am using spring security core plugin in my grails project and the Person domain looks something like this.</p>
  
  <pre><code>class User {
  
  transient springSecurityService
  
  //Mandatory Fields
  String employeeId
  String firstName
  String lastName
  String password
  String emailId
  
  //Other Fields
  String department
  String extn
  
  String mobileNumber
  String address
  String city
  String zipCode
  String country
  
  User manager
  
  static hasMany = [previousPasswords: String]
  
  boolean enabled = true
  boolean accountExpired
  boolean accountLocked
  boolean passwordExpired
  
  static transients = ['springSecurityService']
  
  static constraints = {
      employeeId blank: false, unique: true
      firstName blank: false
      lastName blank: false
      password blank: false, password: true, validator: {val, obj -&gt;
          if(obj.previousPasswords) {
              println "-----------------------1-------------------------"
              println "obj.previousPasswords: " + obj.previousPasswords
              println "val: " + val
              if (obj.isDirty('password')) {
                  println "-----------------------2-------------------------"
                  if(obj.previousPasswords.contains(val)) {
                      obj.errors.rejectValue(
                          'password',
                          'user.password.duplicated',
                          'Repeat passwords are not allowed')
                      return false
                  }
                  println "-----------------------3-------------------------"
                  obj.addToPreviousPasswords(val)
                  return true
              }
          } else {
              println "-----------------------4-------------------------"
              obj.addToPreviousPasswords(val)
              return true
          }
      }
      emailId blank: false, email: true
  
      department nullable: true
      extn nullable: true
  
      mobileNumber nullable: true
      address nullable: true
      city nullable: true
      zipCode nullable: true
      country nullable: true
  
      manager nullable: true
  }
  
  static mapping = {
      password column: '`password`'
  }
  
  Set&lt;Role&gt; getAuthorities() {
      UserRole.findAllByUser(this).collect { it.role } as Set
  }
  
  def beforeInsert() {
      println "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
      encodePassword()
  }
  
  def beforeUpdate() {
      if (isDirty('password')) {
          println "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
          encodePassword()
      }
  }
  
  protected void encodePassword() {
      password = springSecurityService.encodePassword(password)
  }
  }
  </code></pre>
  
  <p>I have to ensure that passwords are not repeated for which I have written a custom validator for 'password' field</p>
  
  <p>My Integration test looks like this</p>
  
  <pre><code>void "Test if previous passwords can be re-used"() {
      given:
      def user = new User(employeeId: "empId1", firstName: "f_name", lastName: "l_name", password: "password", emailId: "test@hptest.com", mobileNumber: "(111) 111-1111", address: "350 Fifth Avenue, 34th floor", city: "New York", zipCode: "10007")
      println "&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;  1  &amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;"
      user.springSecurityService = springSecurityService
      println "&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;  2  &amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;"
      user.save(flush: true, failOnError: true)
  
      when: 'password is repeated'
      user.password = "password"
      println "&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;  3  &amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;"
      user.save(flush: true)
  
      then: 'validation fails'
      !user.validate()
      user.errors.getFieldError("password").codes.contains("validator.invalid")
  
      when: 'password is not repeated'
      user.password = "password@123"
  
      then: 'validation succeeds'
      println "&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;  4  &amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;"
      user.validate()
      println "&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;  5  &amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;"
      user.save(flush: true)
  }
  </code></pre>
  
  <p>and the output I am getting is like this</p>
  
  <pre><code>&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;  1  &amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;
  &amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;  2  &amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;
  -----------------------4-------------------------
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  -----------------------1-------------------------
  obj.previousPasswords: [password]
  val: $2a$10$mxFM9S7mEoOdyRo6xJL9/uzVafUgc0.8r1rd4nzBYM45s2cE8TTDi
  &amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;  3  &amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;
  -----------------------1-------------------------
  obj.previousPasswords: [password]
  val: password
  -----------------------2-------------------------
  -----------------------1-------------------------
  obj.previousPasswords: [password]
  val: password
  </code></pre>
  
  <p>If you carefully study the code and the output you would find that the validator function is called twice for every save operation.</p>
  
  <p>Can anyone tell me what is the reason for this. Could this be due to the addTo method that I am using within validator?</p>