Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
1Activity
0 of .
Results for:
No results containing your search query
P. 1
7. Validation

7. Validation

Ratings: (0)|Views: 9|Likes:
Published by niranjanrath
Grails
Grails

More info:

Published by: niranjanrath on May 02, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

05/01/2010

pdf

text

original

 
The Grails Framework - Reference Documentation
7. Validation
Grails validation capability is built onSpring's Validator APIand data binding capabilities. However Grails takes this further and provides a unified way to define validation "constraints" with itsconstraints mechanism.Constraints in Grails are a way to declaratively specify validation rules. Most commonly they areapplied to domain classes, however URL MappingsandCommand Objectsalso support constraints.
7.1 Declaring Constraints
Within a domain class a constraintsare defined with the constraints property that is assigned a code  block:
 
class User {
 
Stringlogin
 
Stringpassword
 
Stringemail
 
Integerage
 
static
constraints = {
 
}}
You then use method calls that match the property name for which the constraint applies incombination with named parameters to specify constraints:
class User {
 
...
 
static
constraints = {
 
login(size:5..15, blank:
false
, unique:
true
)
 
password(size:5..15, blank:
false
)
 
email(email:
true
, blank:
false
)
 
age(min:18, nullable:
false
)
 
}}
In this example we've declared that the
login
property must be between 5 and 15 characters long, itcannot be blank and must be unique. We've all applied other constraints to the
password
,
email
and
age
properties.A complete reference for the available constraints can be found on the reference1
 
The Grails Framework - Reference Documentationguide
7.2 Validating Constraints
Validation Basics
To validate a domain class you can call the validatemethod on any instance:
def user =
new
User(params)
if
(user.validate()) {
 
//
do
something with user}
else
{
 
user.errors.allErrors.each {
 
println it
 
}}
The
errors
property on domain classes is an instance of the SpringErrorsinterface. The
Errors
interface provides methods to navigate the validation errors and also retrieve the original values.
Validation Phases
Within Grails there are essentially 2 phases of validation, the first phase is data bindingwhich occurs when you bind request parameters onto an instance such as:
 
def user =
new
User(params)
At this point you may already have errors in the
errors
property due to type conversion (such asconverting Strings to Dates). You can check these and obtain the original input value using the
Errors
API:
if
(user.hasErrors()) {
 
if
(user.errors.hasFieldErrors("login")) {
 
println user.errors.getFieldError("login").rejectedValue
 
}}
The second phase of validation happens when you call validateor  save. This is when Grails will validate the bound values againts theconstraintsyou defined. For example, by default the persistentsavemethod calls
validate
before executing hence allowing you to write code like:
if
(user.save()) {
 
return
user
2
 
The Grails Framework - Reference Documentation
}
else
{
 
user.errors.allErrors.each {
 
println it
 
}}
7.3 Validation on the Client
Displaying Errors
Typically if you get a validation error you want to redirect back to the view for rendering. Once thereyou need some way of rendering errors. Grails supports a rich set of tags for dealing with errors. If yousimply want to render the errors as a list you can userenderErrors:
<g:renderErrors bean="${user}" />
If you need more control you can usehasErrorsandeachError :
<g:hasErrors bean="${user}">
 
<ul>
 
<g:eachError var="err" bean="${user}">
 
<li>${err}</li>  </g:eachError>
 
</ul></g:hasErrors>
Highlighting Errors
It is often useful to highlight using a red box or some indicator when a field has been incorrectly input.This can also be done with thehasErrorsby invoking it as a method. For example:
<div class='value ${hasErrors(bean:user,field:'login','errors')}'>
 
<input type="text" name="login" value="${fieldValue(bean:user,field:'login')}"/></div>
What this code does is check if the
login
field of the
user
bean has any errors and if it does adds an
errors
CSS class to the
div
thus allowing you to use CSS rules to highlight the
div
.
Retrieving Input Values
Each error is actually an instance of theFieldError  class in Spring, which retains the original input value within it. This is useful as you can use the error object to restore the value input by the user usingthe fieldValuetag: 3

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->