grails custom validation

18

Upload: to-the-new-technology

Post on 18-Jan-2017

4.144 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Grails Custom Validation
Page 2: Grails Custom Validation

Custom Validation

Page 3: Grails Custom Validation

What is validation ?

● Business rules that constrain the valid values of a particular property in a

domain class

● For example

- A Person must never have an age that is less than zero

- Rules like these should be expressed clearly, and in only one place

Page 4: Grails Custom Validation

Limitation to built-in validators

It is impossible to foresee every feasibledomain

model and every specific kind of validation

that an application might need

Page 5: Grails Custom Validation

Limitation to built-in validators Cont...

• Forexample, Let'sconsider the case withPassword validator:

− should be minimum of specified length− should not contain any field from personal information(name, email,

date

of birth, contact no. etc)

− should use mix of alphabets, digits and special characters( a-z A-Z 0-9

$#@^&*)

− confirm password fieldmust match

Page 6: Grails Custom Validation

Limitation to built-in validators Cont...

• Custom error message depending on the invalid data entered.

− Password must be 8 characters long

− Password should not contain your name

− confirm password do not match

− Weak password, use digits and special characters.

Page 7: Grails Custom Validation

Example 1 (Built-in Validator)

class Employee{

String namestatic constraint ={

name(blank: false)

}

}

Page 8: Grails Custom Validation

Using Custom Validator

class Employee{String namestatic constraint ={Name(validator: {//closure with one, two or three parameter//return value determines the validation//null or true to indicate that the value is valid// false to indicate an invalid value})}}

Page 9: Grails Custom Validation

Validator Closure parameter

• A single or no parameter block receives the value

• A two-parameter block receives the value and object reference

• A three-parameter Closure receives the value, object reference and

the errors object

Page 10: Grails Custom Validation

One Parameter Closure

class User {String loginstatic constraints

={ login(validator: {

// it contains new value for login fieldprintln propertyNameif (!it.startsWith('boba')) return ['invalid.bountyhunter']

})}

}

Page 11: Grails Custom Validation

Two Parameter Closure

class User { String login String password

static constraints ={ password(validator: {newValue, obj ->

println newValue println obj.class.nameprintln obj.properties['login']if(obj.properties['login'

].contains(newValue

))return 'Error: Password contains login name'

})}}

Page 12: Grails Custom Validation

Three Parameter Closure

class User { String login String password

static constraints ={

password(validator: {newValue, obj, err

->println newValue

//entered value for password

println obj.class.name //User println obj.properties['login'] //entered value

for login println err

if(obj.

properties['login'].contains(newValue

))return

'Error: Password contains login name'

})

}}

Page 13: Grails Custom Validation

Validator Closure return value

• Null or true to indicate that the value is valid

• False to indicate an invalid value and use the

default message code

Page 14: Grails Custom Validation

Validator Closure return value Cont...

• A string to indicate the error code to append to the "classname.propertName." string used to resolve the error message. If a field specific message cannot be resolved, the error code itself will be resolved allowing for global error messages

• A list containing a string as above, and then any number of arguments following it, which can be used as formatted message arguments indexed at 3 onwards. See grails-app/i18n/message.properties to see how the default error message codes use the arguments

Page 15: Grails Custom Validation

Custom Error Message

• Error messages can be defined in

grails-app/i18n/message.properties

file in the following format :

errorcode=error message

• e.g.

custom.error=Error occured, property={0} class= {1}value= {2}arg1={3}

• Now you can return any of these error code whose corresponding

error message wil be rendered on views(<g:hasError....tag)

Page 16: Grails Custom Validation

Custom Error Message Cont...

• In error messages you can use the following arguments that are passed

automatically:

−{0}for propertyName e.g. Login

− {1}for Domain class name e.g. class Employee

− {2}for value entered for the validation field

− {3}for 1st argument you pass along with errorcode−{4}for 2nd argument you pass with errorcode

And soon

Page 17: Grails Custom Validation

Example with Custom Error

class User { String userId String password String password2static transients =['password2']static constraints ={

password(blank: false, nullable: false, size:5..20, validator:{password, obj ->

def password2 =obj.properties['password2']if(password2 ==null) return true /* skip matching password

validation(only important when setting/resetting pass) */

password2 ==password ? true : ['invalid.matchingpasswords']

})}}

Page 18: Grails Custom Validation

Contact us

Our Office

Client Location

Click Here To Know More!

Have more queries on Grails? Talk to our GRAILS experts Now!

Talk To Our Experts

Here's how the world's biggest Grails team is building enterprise applications on Grails!