Grails makes validation of user input really simple.
Given virtually any Domain Class or Command Object, you can quickly and easily work up a form and validate it in no time.
The project I'm currently working on ( at work ) requires something above and beyond the normal validation routine - at least compared to most of the normal projects I have on my plate.
I'm at the point where I have the majority of the logic and flow taken care and there's no real validation of user input. That's intentional. I usually implement this as one of the last things so that I can move through the app without any barriers or having to fill each form out in its entirety.
So now I'm at the point where I start implementing validation and editing the default error messages. I have a form validation requirement that is dependent upon the values of other form fields. Since I haven't had this as a requirement for other apps in the past ( with Grails ) I decide to take a look at the documentation at the Grails site.
Crap! They are only documenting pretty simple, straight-forward stuff.
Say you want to validate the age field ( so that it cannot be null ) on a Domain Class or Command Object:
static constraints = {
age(nullable:false)
}
Simple. Straight forward. I like it.
But my situation is considerably different.
In my situation the user must complete a form that constitutes a business address. Also on the form are fields that make up a contact name, title and phone number. The business rule states that if the form is submitted with a Contact Name or Contact Title there must be a Phone Number.
It would be nice if there were a built-in constraint like:
static constraints = {
field1(mustBePresent:field2)
}
If I remember right Rails has something like this. I can't remember the name of the validator in Rails, but there's a way to do this with Grails . . .
It took some searching and most of the examples I found were posts to forums in which the implementation of the validation was not working correctly. While the posts gave me some insight, I had to try and figure out what was wrong with their code and how to get it working correctly.
Essentially what I want is validation on both the Contact Name and Contact Title fields. The validation should confirm that the Phone Number field is populated in the event that either the Contact Title or Contact Name fields are populated. For this example I'm not going to go into validating the content of the values, just the presence of.
The message can be essentially the same in the messages.properties file, but we'll need two entries.
className.contactName.invalid.phone=A Phone Number must accompany a Contact Name.
className.contactTitle.invalid.phone=A Phone Number must accompany a Contact Title.
Here's what I came up with:
constraints = {
. . . . .
phoneName( validator: { val, obj ->
if(
( val != null && val.length() > 0 ) &&
( (obj.properties['phone'] == null) || (obj.properties['phone'].length() <= 0 )
)
{
return ['invalid.phone']
}
})
phoneTitle( validator: { val, obj ->
if(
( val != null && val.length() > 0 ) &&
( (obj.properties['phone'] == null) || (obj.properties['phone'].length() <= 0 )
)
{
return ['invalid.phone']
}
})
}
I could just as easily performed other validation checking for a valid phone number with regex, although that should probably done directly on the phone number field.
A good practical application of this is checking a password confirmation field. Perhaps I'll do that in another post.
I hoped this helped if you came across this post looking for an example like this - let me know in the comments.

If you don't recognize the picture or the reference to the ultimately awesome movie, "Office Space" you are missing out.
Recent Comments