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?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Pat
  • 2,228
  • 3
  • 24
  • 33
  • 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. – Rob Hruska Oct 26 '10 at 13:22
  • 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. – Pat Oct 26 '10 at 14:41

1 Answers1

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

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192