grails basics part2

21
Grails Basics 2 By: Saurabh Dixit

Upload: saurabh-dixit

Post on 17-May-2015

435 views

Category:

Technology


3 download

DESCRIPTION

Basics in grails: Groovy server pages Taglibs Validators in grails

TRANSCRIPT

Page 1: Grails basics  part2

Grails Basics 2

By: Saurabh Dixit

Page 2: Grails basics  part2

Agenda:

Groovy server pages Taglibs Validators in grails

Page 3: Grails basics  part2

Groovy Server Pages

View pages with the extension gsp, gsp stands for groovy server page

All default groovy tags along with custom tags defined in the tag lib can be accessed in these pages.

Page 4: Grails basics  part2

Tag Libraries:

Tags can be created inside taglib folder in grails:

class WidgetsTagLib {

static namespace = “appTags"

ApplicationContext applicationContext

/** * Extends the regular grails pagination */def paginate = { attrs ->// DO some operation }

Tags inside taglib can be accessed on the GSP pages through the name space:<appsTags:paginate/>

appsTags: Name space defined in the tag libPaginate: Method in the file

Page 5: Grails basics  part2

Validators in grails :

Declaring Constraints: Within a domain class constraints are defined

with the constraints property that is assigned a code block:

class User { String login String password String email Integer age static constraints = { … }}

Page 6: Grails basics  part2

You then use method calls that match the property name for which the constraint applies in combination with named parameters to specify constraints:

class User {...static constraints = {login size: 5..15, blank: false, unique: truepassword size: 5..15, blank: falseemail email: true, blank: falseage min: 18}}

Page 7: Grails basics  part2

In that example we've declared that the login property must be between 5 and 15 characters long, it cannot be blank and must be unique. We've also applied other constraints to the password, email and age properties.

By default, all domain class properties are not nullable (i.e. they have an implicit nullable:false constraint).

Page 8: Grails basics  part2

A complete reference for the available constraints can be found in the Quick Reference section under the Constraints heading.

class User {...static constraints = {// this Date object is created when the constraints are evaluated, not// each time an instance of the User class is validated.birthDate max: new Date()}}Custom validator:class Response {…static constraints = {survey blank: falseanswer blank: false, validator: { val, obj -> val inobj.survey.answers }}}

Page 9: Grails basics  part2

Validating Constraints: Call the validate method to validate a domain class

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 Spring Errors interface. The Errors interface provides methods to navigate the validation errors and also retrieve the original values.

Page 10: Grails basics  part2

Validation Phases:

Within Grails there are two phases of validation, the first one being data binding which occurs when you bind request parameters onto an instance such as:

def user = new User(params)

Page 11: Grails basics  part2

At this point you may already have errors in the errors property due to type conversion (such as converting 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}}

Page 12: Grails basics  part2

The second phase of validation happens when you call validate or save. This is when Grails will validate the bound values againts the constraints you defined.

For example, by default the save method calls validate before executing, allowing you to write code like:

if (user.save()) {return user}else {user.errors.allErrors.each {println it}}

Page 13: Grails basics  part2

Importing Constraints: Grails 2 introduced an alternative approach to sharing

constraints that allows you to import a set of constraints from one class into another.

Let's say you have a domain class like so:class User {String firstNameString lastNameString passwordHashstatic constraints = {firstName blank: false, nullable: falselastName blank: false, nullable: falsepasswordHash blank: false, nullable: false}}

Page 14: Grails basics  part2

You then want to create a command object, UserCommand, that shares some of the properties of the domain class and the corresponding constraints. You do this with the importFrom() method:

class UserCommand {String firstNameString lastNameString passwordString confirmPassword

static constraints = {importFrom Userpassword blank: false, nullable: falseconfirmPassword blank: false, nullable: false

}}

Page 15: Grails basics  part2

If you want more control over which constraints are imported, use the include and exclude arguments.

Both of these accept a list of simple or regular expression strings that are matched against the property names in the source constraints.

Page 16: Grails basics  part2

So for example, if you only wanted to import the 'lastName' constraint you would use:

…static constraints = {importFrom User, include: ["lastName"]…}

or if you wanted all constraints that ended with 'Name':

…static constraints = {importFrom User, include: [/.*Name/]…}

Page 17: Grails basics  part2

Validation on the Client: Displaying Errors:▪ Typically if you get a validation error you

redirect back to the view for rendering. Once there you need some way of displaying errors.▪ Grails supports a rich set of tags for dealing

with errors.▪ To render the errors as a list you can use

renderErrors:<g:renderErrors bean="${user}" />

Page 18: Grails basics  part2

If you need more control you can use hasErrors and eachError:<g:hasErrors bean="${user}"><ul><g:eachError var="err" bean="${user}"><li>${err}</li></g:eachError></ul></g:hasErrors>

Page 19: Grails basics  part2

Applying Validation to Other Classes: Domain classes and command objects support

validation by default. Other classes may be made validateable by

defining the static constraints property in the class (as described above) and then telling the framework about them.

It is important that the application register the validateable classes with the framework. Simply defining the constraints property is not sufficient.

Page 20: Grails basics  part2

The Validateable Annotation: Classes which define the static constraints property

and are annotated with @Validateable can be made validateable by the framework.

Consider this example:// src/groovy/com/mycompany/myapp/User.groovypackage com.mycompany.myappimport grails.validation.Validateable@Validateableclass User {...static constraints = {login size: 5..15, blank: false, unique: truepassword size: 5..15, blank: falseemail email: true, blank: falseage min: 18}}

Page 21: Grails basics  part2

Thanks….