Handling validation on Entity properties

Grails GORM (Grails Object Relational Mapping) allows you to define a number of validation rules that are carried through to your view when entering data.

For example with following simple Entity:

<code>
class Test1 { 
    Long id
    Long version
    String string1
    Long testLong
    Float float1
	
    def constraints = {
        string1(blank:false)
        testLong(nullable:false,blank:false)
        float1(nullable:false,blank:false)
	}
...
}
</code>

The ‘constraints’ section defines the rules on the properties on this Entity – the rules are a comma separated list of name/value pairs for the rules to be applied. Notice the ‘blank’ only works on String properties. To require a value for any other property type, use ‘nullable:false’.

Here are some further self-explanatory examples from the online Grails docs:

<code>

   def constraints = {
          login(length:5..15,blank:false,unique:true)
          password(length:5..15,blank:false)
          email(email:true,blank:false)
          age(min:new Date(),nullable:false)
   }
</code>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.