Grails Custom Validation - Request inside validation validation - What happens when upgrading?

I have a special validator, for example -

validator: { userEmail, userAccount ->

   if (userAccount.authenticationChannel == "ABC") {
      boolean valid = true;
      UserAccount.withNewSession {
      if (UserAccount.findByEmail(userEmail)){
         valid = false;
      }
      else if (UserAccount.findByName(userEmail)) {
         valid = false;
      }

...

So basically, I need a check based on some particular state, and in my check I need to execute a query.

But now, if I do -

def admin = new UserAccount(firstname:'Admin',email:'admin@example.com')


admin.save(flush:true)


admin.addToAuthorities("ADMIN").save(flush:true)

Fails.

Grails starts validation, even when upgrading, and since email authentication fails. How is this different if I do

email {unique:true}

Does Grails say that I cannot write a special validator that checks for uniqueness.

+5
source share
3 answers

, , ​​ (, , ), StackOverflowError. , (, findByEmail) Hibernate , , , .

, , , "" . Hibernate . , , , () .

UserAccount.withNewSession { session ->
    session.flushMode = FlushMode.MANUAL
    try {
        if (UserAccount.findByEmail(userEmail)){
            valid = false;
        }
        else if (UserAccount.findByName(userEmail)) {
            valid = false;
        }
    }
    finally {
        session.setFlushMode(FlushMode.AUTO);
    }
}

. UniqueConstraint , .

+6

.

def save = {
  ..
  if (some_checks_succeed(userEmail, userAccount)) {
    admin.save(flush: true)
  }
  ..
}

def some_checks_succeed = { String userEmail, String userAccount ->
  boolean valid = true;
  if (userAccount.authenticationChannel == "ABC") {
    UserAccount.withNewSession {
    if (UserAccount.findByEmail(userEmail)) {
     valid = false;
    } else if (UserAccount.findByName(userEmail)) {
     valid = false;
    }

    ..
  }

  return valid
}

,

0

. . admin.save() , . ( ) .

0

All Articles