viernes, 23 de abril de 2010

Grails Validation Plugin ( ValidationField Plugin )

This plugin allow us to render input (text/password),textArea and select tag forms.
When the user send the form, if there are domain class errors, the form element tags change their CSS style, showing the error (and the message).
Your i18n default message can be showed or not, just by setting the property showMessage to "true" or "false".
The CSS style for the form elements and the error message are both customizables.

How to install it?

On your application command line type: 

grails install-plugin validation-field

Or download the plugin from here and type:

grails install-plugin [dir]/

Thats all

Now, how to start to work with it?
In the gsp file, between the <head> tags, put this line ( to import the required css file ): 

<g:validationFieldIncludes />

Now, you can use the  <g:validationField /> and the <g:validationSelect /> tags instead the <input .../> and <select> tags.

Usage for render input and textArea form elements <g:validationField ... />
Are required just three properties in your <g:validationField /> tag:

tag: The type of html tag ( input or textarea ).
domain: The domain object (just like this ${myObject}).
field: The name of the object's field to evaluate.


<g:validationField tag="textarea" domain="${person}" field="name" />

This line will show a textArea html element in your html rendered page, when the user send the form (if the field 'name' is empty and in your domain class it was setted as mandatory ), the gsp now will rendered with the same textArea, but his CSS Style will change for one more visible (red colored), and your i18n message will be shown ( in red color, too ) just under the textarea.

<g:validationField tag="input" type="text"  domain="${person}" field="name" />

Will show the same, but in a input text form element.

You can use all the html properties ( style, width, height, class, etc ) in the <g:validationField> tags (it works with password input tags)

Usage for render select form elements <g:validationSelect ... />

The usage of the validationSelect tag is the same as the Grails <g:select> tag, you must use the same properties, plus the ValidationField Plugin properties: domain and field.


<g:validationSelect domain="${person}" field="country" from="${Country.list()}" value="${}" optionKey="id" optionValue="name" />

The 'domain' and 'field' properties are ValidationPlugin properties, the rest are Grails <g:select /> properties.

Note: The <g:select> properties are in:

Advanced usage (for both validationField and validationSelect tags):
Other properties:

value:By default value to be shown.
messageErrorClass: CSS class to overwrite the plugin CSS class by default to use in the error/validation message.
fieldErrorClass: CSS class to overwrite the plugin CSS class by default to use in the error/validation form field.
customErrorMessage: A message to be shown if you don't want to show the grails validation default message.
showMessage: String (true or false) that indicate if you want to show the validation message of just the field with the style changed.