Beginning Groovy, Grails and Griffon by Vishal Layka & Christopher M. Judd & Joseph Faisal Nusairat & Jim Shingler

Beginning Groovy, Grails and Griffon by Vishal Layka & Christopher M. Judd & Joseph Faisal Nusairat & Jim Shingler

Author:Vishal Layka & Christopher M. Judd & Joseph Faisal Nusairat & Jim Shingler
Language: eng
Format: epub
ISBN: 9781430248064
Publisher: Apress


Of course, it is impossible for the Grails team to predict all the possible constraints needed by an application. Using pre-created constraints is the easy part. In the next section, we’ll show you how to create your own customized constraints using closures.

Creating Custom Constraints

Let’s take another look at that Todo object and what other constraints you need to define for it. Let’s examine the startDate property and see what you need to constrain on it. For starters, you don’t want to allow users to use start dates in the past. The purpose of this application is to create tasks that start now or in the future; in theory, this is something that is probably created at the beginning and never changed. Nothing in the built-in constraints shown in Table 6-2 does what you need, so you need to create your first custom constraint. You need to allow the constraint to be null, and if the constraint is filled in, you need to make sure the date doesn’t occur in the past.

To define custom constraints in GORM, mark the custom constraint with a validator. Take a look at this in action in Listing 6-20.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.