Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Table of Contents

What is

...

Griffon Validation

GValidation is a validation plugin for Griffon - A Grails like application framework for developing desktop applications in Groovy. Like most part of the Griffon framework GValidation's syntax and usage closely resemble its cousin Grails' validation and constraints support. However the similarity pretty much stops here, since Griffon model are very different from Grails' concept of model. In Grails model usually refers to domain classes that are bound to database, however in Griffon models are usually just mere POGOs thus this plugin is designed to work with Griffon models as well as any POGOs.

GValidation is written purely in Groovy while retaining most of the syntax of Grails constraints support.

History

Version    

Description

Released On    

1.0Upgraded to Griffon 1.0.1July 12, 2012
0.9Updated to Griffon 0.9.5 with bug fixesApr 8, 2012

0.8.2

Updated to Griffon 0.9.4

Oct 30, 2011

0.8.1

Fixed multi-error rendering issue

Sept 10, 2011

0.8

Updated to Griffon 0.9.3 with RT support

August 19, 2011

0.7.1

Bug fix minor release

May 13, 2011

0.7

Introduced error renderer and widgets    

May 04, 2011

0.6

Updated to Griffon 0.9.2

Feb 22, 2011

0.5

Updated to Griffon 0.9

Jul 20, 2010

Project Page

GitHub Project Page

...

GValidation plugin depends on the following libraries, and will automatically add them to your application once the plugin is installed.

Code Block
Griffon i18n Plugin
Apache Commons Lang 2.5
Apache Commons Validator 1.3.1
Jakarta ORO 2.0.8

...

Code Block
personModel.email.blank.message

Default Error Message Code

...

Each validator built-in or custom also has a global default error message code associated with in the following format:

...

Code Block
error.errorCode
error.defaultErrorCode

Currently due to an issue i18n-supoprt plugin does not load the default messages.properties file shipped with validation plugin therefore you will need to add the following message definition manually in your messages.properties file.

default.matches.message=Property [{0}] of class [{1}] with value [{2}] does not match the required pattern [{3}]

default.url.message=Property [{0}] of class [{1}] with value [{2}] is not a valid URL

default.creditCard.message=Property [{0}] of class [{1}] with value [{2}] is not a valid credit card number

default.email.message=Property [{0}] of class [{1}] with value [{2}] is not a valid e-mail address

default.range.message=Property [{0}] of class [{1}] with value [{2}] does not fall within the valid range

default.size.message=Property [{0}] of class [{1}] with value [{2}] does not fall within the valid size

default.max.message=Property [{0}] of class [{1}] with value [{2}] exceeds maximum value [{3}]

default.min.message=Property [{0}] of class [{1}] with value [{2}] is less than minimum value [{3}]

default.maxSize.message=Property [{0}] of class [{1}] with value [{2}] exceeds the maximum size of [{3}]

default.minSize.message=Property [{0}] of class [{1}] with value [{2}] is less than the minimum size of [{3}]

default.validator.message=Property [{0}] of class [{1}] with value [{2}] does not pass custom validation

default.inList.message=Property [{0}] of class [{1}] with value [{2}] is not contained within the list [{3}]

default.blank.message=Property [{0}] of class [{1}] cannot be blank

default.notEqual.message=Property [{0}] of class [{1}] with value [{2}] cannot equal [{3}]

default.nullable.message=Property [{0}] of class [{1}] cannot be null

...

 

Real Time Validation Support

Since v0.8 release validation plugin now can automatically trigger validation for Griffon model beans if a realTime flag is set to true in @Validatable annotation.

...

Many of the above explanation were borrowed directly from Grails reference guide

Constraint

...

Inheritance

Since the constraints are defined using static fields following Grails convention, no real inheritance can be implemented. However since 0.6 release Validation plugin will basically copy the parent class' constraints to the child before performing validation, thus additionally you can also override the parent constraint in the child class. See the following example:

...

In the above example, the ProtocolSpecificServerParameter will not only inherent ServerParameter's serverName and port fields but also their associated constraints. The only restriction you need to be aware of is if the parent constraint generates error for a certain condition then the overriding child constraint has to generate error as well. In other words, validation plugin does not allow error-hiding by using constraint override in the child class, similar to the method exception treatment during inheritance within Java.

Custom Constraint

...

The validator mentioned above allows you to specify a closure as a simple custom constraint easily and quickly however there is no easy way to reuse the closure in other scenarios, hence you will be forced to rewrite the validator each time you use them which is inconvenient and a violation of the DRY principle. Since version 0.3, inspired by Grails Custom Constraint plugin, GValidation plugin now provides you ways to define reusable custom constraints in Griffon.

...

Code Block
model.validate()
    ...
if(model.hasErrors()){
    // notify user
    ...
}

Selective Validation

...

Originally proposed by Andres Almiray, since v0.3 GValidation now offers capability to perform validation on only a selected number of fields in the model instead of all. Here is a typical single field validation usage scenario:

...

Code Block
model.validate(['name', 'email'])
...
if(model.hasErrors()){
    // notify user
    ...
}

Before Validation Callback

...

Inspired by Rails before_validation callback, now GValidation provides a similar pre-validation callback to give model developer a chance to manipulate data right before validation. Here is an example how this kind of callback is defined:

...

Code Block
model.errors.each{error->
    // do something with the error
}

Binding Errors

...

Since 0.4 release the dynamic errors field has been enhanced to be Bindable which means you can now directly bind it to your component. It is especially handy when building error notification component such as in the built-in ErrorMessagePanel. Here is how the binding can be achieved in the view with the built-in panel:

Code Block
container(new ErrorMessagePanel(messageSource),
                    id: 'errorMessagePanel', constraints: NORTH,
                    errors: bind(source: model, 'errors'))

i18n

...

Since v0.5 release Validation plugin now is officially depend on i18n plugin, if your project does not have this plugin installed adding Validation plugin will result i18n plugin being added to your project automatically. GValidation is shipped with a simple generic errorMessages widget to help you display error message easily. Of course you can build your own error message feedback component, it is fairly easy to do that, check out the source code for the built-in ErrorMessagePanel for more details. To use the built-in error panel first declare it in your view:

...

Enhance POGO with Validation Support

Use @Validatable AST Transformation

...

Since 0.4 release now you can enhance any POGO class in your application by adding the @Validatable annotation at the class level, then Groovy AST transformation will take care of the rest.

...

This kind of annotated classes will go through essentially the same enhancement as any model class. The only difference is that annotated class is enhanced during build time using AST transformation vs. runtime enhancement as what happen to the model instances.

Error Renderer

...

One of the common challenge we face when building UI using any GUI framework is how to effectively and easily notify the user about errors. Ideally a validation framework should not just help developer define constraints and validation logic but also handle the presentation of the error message automatically with little coding involved. With this vision in mind Error Renderer was created.

...

Code Block
errorIcon(errorRenderer:'for: creditCard, styles: [onWithError]')

Screen Shot:

Demo app

A simple demo application can be downloaded from here.