I’m trying to validate a SELECT. Normally I’d use an inList, as SELECT implies a fixed number of strings, but i was wondering if there was something more elegant. In this case, I have a form with a SELECT input that has the values 0-24 as , corresponding to the next 24 months.
In my cmdObject I have
class FormCommand {
String startSlot
static constraints = {
// startSlot(nullable:false, blank:false, range:0..24)
startSlot(nullable:false, blank:false,
validator: { val, obj -> val.toInteger() < 25})
}
}
I’d like to be able to use the range:0..24 statement, but from what I understand of ranges, they doesn’t apply to Strings generated by the form.
Is there a preferred way to either cast/bind the incoming string into an int so I can use the range:0..24? Or is there another way to handle this?
I could do
inList: [ "0", "1", /* type them all out */, "24" ]
or write some more robust custom validators, but I’m wondering if there’s a more groovy/grails solution.
Thanks.
1 Answer