Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fake a validation error?

I'm using the Grails Webflow plugin. Here are the domain objects I'm working with:

class Foo implements Serializable {
    String fooProp1,
           fooProp2

    static constraints = {
        fooProp2 nullable: false
    }
}

class Bar implements Serializable {
    Foo fooObject

    static constraints = {
        fooObject nullable: false
    }
}

At a point in the webflow, I need to make sure that fooObject.fooProp1 is not null. If it is, I want to throw an error and force the user to supply it with a value. I tried using validate() to do this (on both the Bar and Foo objects), but since fooProp1 has the nullable:true property, it passes validation. Any ideas?

like image 291
Pat Avatar asked Oct 26 '10 12:10

Pat


1 Answers

You can probably do this in the Web Flow by adapting the following code:

if(fooObject.fooProp1 == null) {
    fooObject.errors.rejectValue('fooProp1', 'nullable')
}

The second argument to that method, 'nullable', might be different for your situation. You'll just need to set it to the message code (from message.properties) to display the error message that you want.

Have a look here for more ways to use reject() and rejectValue().

like image 88
Rob Hruska Avatar answered Sep 21 '22 01:09

Rob Hruska