How do I fake a validation error?
Asked Answered
A

1

5

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?

Aminopyrine answered 26/10, 2010 at 12:50 Comment(2)
I'm a bit confused - if this is the case, shouldn't the property just be nullable: false? Can you clarify why you can't add the constraint to your domain? That's the most obvious solution to me, I guess.Janot
Well, the Foo objects are allowed to exist in the database without a fooProp1 specified. However, during the webflow, this property needs to not be null, as it is used at the end of the webflow process.Aminopyrine
J
10

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().

Janot answered 26/10, 2010 at 14:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.