Remote Constraints Plugin

  • Tags : validation, ajax
  • Latest : 0.4.3
  • Last Updated: 01 June 2009
  • Grails version : 1.1.1 > *
  • Authors : null
2 votes
Dependency :
compile ":remote-constraints:0.4.3"

Summary

Description

The remote constraints plugin allows you to easily integrate your custom defined javascript validation with the out of the box domain constraints mechanism provided by grails. In particular this plugin allows you to use constraint methods defined on domain classes to perform ajax validation.

Author

Tobia Loschiavo (tobia dot loschiavo at gmail dot com). Please don't hesitate to contact me for suggestions or problems.

Installation

grails install-plugin remote-constraints

Exclude constraints to be ajaxified

From version 0.3 by default every constraint is ajaxified automatically. User, however, can decide what constraint he wants to exclude.For example if he wants to use a javascript validation to check if the value inserted is not blank and the remaining validation types like email validation and minimum size to be executed serverside, all he should do is to add the constraint names he wants to exclude to the Config.groovy file.

remoteValidation.disabledRemoteConstraints = ["blank"]

It is also possible to activate the plugin providing the static property "remoteConstraints" in your custom defined constraints.

static remoteConstraints = false

Usage

Two tags are provided:

<g:remoteConstraintsInput name="example" beanName="Test" fieldName="name" 
template="/shared/error" value="${testInstance.name}" var="errorBean"
updateElement="errorsDiv"/>

This tag creates an input field that validates its value using the constraints of the provided couple of value beanName/fieldName that contain respectively the name of the domain class and the name of the field. Template attribute provides the name of the template that should be used to show errors. The attribute "var" tells the tag the name of the variable used in the template and the "updateElement" attribute specifies the id of the element to be updated with the template content. In that tag a "updateFunction" attribute is also provided to use a javascript function to process the ajax data. This could be the template used in the example above:

<g:hasErrors bean="${errorBean}">
    <div class="errors">
        <g:renderErrors bean="${errorBean}" as="list" />
    </div>
</g:hasErrors>

If you want to use only the ajax function you have to use the "remoteConstraints" tag:

<g:remoteConstraints beanName="Test" fieldName="name" 
template="/shared/error" var="errorBean"
updateElement="errorsDiv"/>

This tag generates the javascript needed to call remote validation. Clearly tags can be used also as functions:

<input id="inputManual" onblur="${remoteConstraints(beanName:"Test", 
fieldName:"name", var:"errorBean", template:"/shared/error", 
updateElement:"errorsDiv")}" />

These are the available attributes for this tag:

  • beanName: (required) it is the name of the bean you are planning to validate your fields against.
  • fieldName: (required) it is the name of the field you want to validate remotely.
  • template: the template used to provide errors
  • var: the var used in the template
  • updateElement: id of the container you want to update with error information
  • updateFunction: the function used to update error container. The parameters available in this function depends on the javascript provider you chose. For example for jquery you can use in the function the parameter 'data' that will contain the html error.
  • inputFunction: since 0.4 version, you can use this attribute to put the javascript function needed to send other fields of the object to perform, for example, a cross field validation. (with jquery for example we could use: function(){return jQuery('#formtest').serialize();} )
  • excludedValidators: (since 0.4.3) with this comma separated value, you can exclude the contraints you don't want to be processed through this plugin. It is the same set you can put in config.groovy but it is at field level. You can combine the global setting and the field one to achieve your objectives.

Changelog

0.4.3: code refactoring and new attribute excludedValidators

0.4: added cross fields validation