seemlyMike
seemlyMike

Reputation: 55

Grails and spring security core plugin - editing user

I'm having problem with validation while editing user. Everything works ok but each time I want to edit selected user I'm getting validation error that second password(password confirmation) is null. I know it is null because I'm not storing password confirmation in my database. My app has almost same User class that is in Spring Security Core plugin for Grails Documentation, I haven't changed anything in default UserController.

How I can turn off that validation message?

Upvotes: 2

Views: 101

Answers (2)

Koloritnij
Koloritnij

Reputation: 1217

You have passwordConfirmation field in your domain class with constraint:

passwordConfirmation bindable: true, blank: false, password: true, display: false, validator: { val, obj ->
            if (obj.id && (!obj.password )) {
                return true
            }
            if (!(obj.password == obj.passwordConfirmation)) {
                return 'passwordMismatch'
            }
            return true
        }

it can't be 'blank' and must be same as 'password'. So, you have 2 ways:

  1. Remove passwordConfirmation from domain class and run migration script(if use).
  2. Changed constraint to passwordConfirmation nullable: true and start migration script or update base by your hand(need to remove not null constrain).

Upvotes: 1

Vinay Prajapati
Vinay Prajapati

Reputation: 7504

In your case it is more dependent upon your business logic. If I assume that you need confirmPassword on create page but on edit page you don't need it, I would say use CommandObjects.

Remove confirmPassword from your domain. Create a command object with @Validatable annotation. Do write your custom validation for confirm password there. Adjust it with your needs. Also, you could have more than one command objects for a domain or we can say these are independent of domains and could be used for any custom validation or manipulations.

Would recommend going through this to get understanding of command objects and their usage.

For your case, a co(in short) could be like below:

@Validatable
class UserCO {
    def springSecurityService

    String username
    String password
    String passwordConfirmation
    boolean enabled = true
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired

static constraints = {
        username blank: false, unique: true
        password blank: false, size: 5..60, password: true, display: false, validator: { val, obj ->
            if (!obj.id && !obj.password) {
                return 'validation.user.password'
            }
        }
        passwordConfirmation bindable: true, blank: false, password: true, display: false, validator: { val, obj ->
            if (obj.id && (!obj.password )) {
                return true
            }
            if (!(obj.password == obj.passwordConfirmation)) {
                return 'passwordMismatch'
            }
            return true
        }
    }
}

Hope it helps!

Upvotes: 1

Related Questions