Last updated by 5 years ago

Page: Search forms scaffolding, Version:0

Proposal

While Grails provides basic views and controllers scaffolding for typical CRUD actions, many typical web applications require some kind of a search functionality in one form or another. In the typical scenario, a search would need to be performed based on some (or all) of the properties of domain object(s). This is done by presenting a "search form" view backed by a 'search' action in controller (perhaps dynamically build a query, based on the user input into the search form, using Hibernate Criteria API (GORM Criteria API facade))

It would be very useful to have a scaffolding mechanism is Grails which could auto-generate search form GSPs based on some conventions or DSL in domain classes, similar to constrains or GORM mapping DSL.

One possible way to declare search form would be:

class Book {
     String name
     String isbn
     Date datePublished
     static belongsTo = [author:Author]

...

//Closure option static searchFields = { isbn datePublished author_name author_age author_address_postalCode }

//Alternative list option static searchFields = ['isbn', 'datePublished', 'authorName', 'author_age', 'author_address_postalCode']

… }

The searchFields data structure should describe which fields should be included in the search.gsp form and corresponding default 'search' action closure in the related controller i.e. BookController. Note that fields with underscore (_) at the end should indicate object graph traversal, etc.

Alternatively, there could be a way to pre-generate default values for the search fields. For example:

class Book {
    String name
    String isbn
    Date datePublished
    static belongsTo = [author:Author]

...

//Generating the default values and building complex search forms static searchFields = { isbn(inList:['12345','23456','7494874']) //Should generate drop down box on the search form datePublished(range:'01/31/2007'..'02/12/2008') //should generate 2 text fields filled with range (to, from) values author_name(default:'Graeme Rocher') //Should generate 1 text field filled with the default value author_age //Empty text field author_address_postalCode //Empty text field }

… }