It just takes 15 seconds. Send your
details and we’ll get back to you soon.

Inject custom validation errors in object

Reading the grails docs is like my habit, they always enhances your learning. Today I was going through the grails docs again and I found a very good way of showing some custom error messages. As I saw it I found the use case in one of my project that could be refactored by this approach

There are use cases where we need to show some errors which are beyond your domain / command object constraints validators for these cases you can inject your custom error messages to the object take an example of updating the password. Any of the field is not domain related so we have to create a command object like

class UpdatePasswordCO {

    String password
    String newPassword
    String confirmNewPassword

    static constraints = {
        password(nullable: false, blank: false)
        newPassword(nullable: false, blank: false)
        confirmNewPassword(nullable: false, blank: false, validator: {val,   obj ->
            if (obj.newPassword != val) {

Now you have to check whether the logged in user entered the correct password or not. So rather than putting the message in flash scope you can directly put this error to the command object it self

def updatePassword = {UpdatePasswordCO updatePasswordCO ->
        if (updatePasswordCO.validate()) {
            if (User.loggedInUser.validatePassword(updatePasswordCO.password)) {
                flash.message = "Password updated successfully"
            } else {
                updatePasswordCO.errors.rejectValue("password", "password.mismatch.current.password")
        if (updatePasswordCO.hasErrors()) {
            render(view: 'profile', model: [updatePasswordCO: updatePasswordCO])
        } else {
            redirect(action: 'profile')

So in above approach you have injected a new error on password field with a code. By doing this your hasErrors tag on gsp page will show your custom error as well. For more details you can refer Spring

Hope it helps
Uday Pratap Singh


Hope it helps
Uday Pratap Singh

7 Responses to Inject custom validation errors in object

  1. Mei says:

    I’d like to thank you for the efforts you have put in penning this
    website. I’m hoping to view the same high-grade blog posts from
    you later on as well. In truth, your creative writing abilities has encouraged me to get
    my very own blog now ;)

  2. coxyde says:

    Hi mates, fastidious article and good arguments commented here, I am in fact enjoying by these.

    My web page; coxyde

  3. Very great post. I simply stumbled upon your blog and wished to mention that I have
    truly enjoyed surfing around your weblog posts.
    After all I will be subscribing to your rss feed and I’m hoping you write again very soon!

  4. Hmm is anyone else having problems with the pictures on this blog loading?
    I’m trying to determine if its a problem on my end or if it’s the blog.
    Any feedback would be greatly appreciated.

    My website: Qu’est-ce qu’on a fait au Bon Dieu ? Télécharger

  5. If some one desires to be updated with hottest technologies
    after that he must be visit this web site and be up to date daily.

  6. And although its sleek, thin design looks technologically
    impressive, is it really the ideal computer that it purports itself to be.

    Alternative macbook stands offer more ancient designs.

    My weblog; uad apollo duo

  7. There is a virtual key board accompanying the incredibly attractive
    touch sensitive display screen. The Xperia X10 measures 119×63x13mm and weighs
    in at 135 grams. It has been predicted that these smartphones will become a necessity for the
    blind in the next few years.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>