(Quick Reference)

3 Customizing Field Rendering - Reference Documentation

Authors: Rob Fletcher

Version: 1.5

3 Customizing Field Rendering

The plugin resolves the GSP template used for each property according to conventions. You can override the rendering based on the class and property name or the property type. The f:field tag looks for a template called _wrapper.gsp, the f:widget tag looks for a template called _widget.gsp, the f:display tag looks for a template called _displayWrapper.gsp.

Breaking changes in version 1.5

In version 1.5 a new template was introduced _displayWidget.gsp. This is the corollary of _widget.gsp for fields that are read-only, i.e. it is responsible for rendering just the markup for the field itself. Furthermore, the default names of all the templates were changed in this version, in the interest of clarity and consistency. The changes to the template names are summarized below:

Old Template Name (before v.1.5)New Template Name (v.1.5 onwards)
_field.gsp_wrapper.gsp
_input.gsp_widget.gsp
_display.gsp_displayWrapper.gsp
N/A_displayWidget.gsp

Users upgrading to 1.5 from a previous version should either rename their templates (recommended) or add the following to Config.groovy to change the default templates names to the old names

grails.plugin.fields.wrapper = "field"
grails.plugin.fields.displayWrapper = "display"
grails.plugin.fields.widget = "input"
grails.plugin.fields.displayWidget = "displayWidget"

Locating Field Templates by Convention

The template for a field is chosen by a convention using the names of the controller, action, bean class, bean property, etc. All the tags will look for templates in the following directories in decreasing order of preference:

  1. grails-app/views/controllerName/actionName/propertyName/
  2. grails-app/views/controllerName/actionName/propertyType/
  3. grails-app/views/controllerName/actionName/
  4. grails-app/views/controllerName/propertyName/
  5. grails-app/views/controllerName/propertyType/
  6. grails-app/views/controllerName/
  7. grails-app/views/_fields/class/propertyName/
  8. grails-app/views/_fields/superclass/propertyName/
  9. grails-app/views/_fields/associationType/
  10. grails-app/views/_fields/propertyType/
  11. grails-app/views/_fields/propertySuperclass/
  12. grails-app/views/_fields/default/

The variables referenced in these paths are:

NameDescription
controllerNameThe name of the current controller (if any).
actionNameThe name of the current action (if any).
classThe bean class. For simple properties this is the class of the object passed to the bean attribute of the f:field or f:widget tag but when the property attribute was nested this is the class at the end of the chain. For example, if the property path was employees[0].address.street this will be the class of address .
superclassAny superclass or interface of class excluding Object , GroovyObject , Serializable , Comparable and Cloneable .
propertyNameThe property name at the end of the chain passed to the property attribute of the f:field or f:widget tag. For example, if the property path was employees[0].address.street then this will be street .
propertyTypeThe type of the property at the end of the chain passed to the property attribute of the f:field or f:widget tag. For example, for a java.lang.String property this would be string .
propertySuperclassAny superclass or interface of propertyType excluding Object , GroovyObject , Serializable , Comparable and Cloneable .
associationTypeOne of 'oneToOne' , 'oneToMany' , 'manyToMany' or 'manyToOne' . Only relevant if the property is a domain class association.

All class names are camel-cased simple forms. For example java.lang.String = string , com.project.HomeAddress = homeAddress .

Templates are resolved in this order so that you can override in the more specific circumstance and fall back to successively more general defaults. For example, you can define a field template for all java.lang.String properties but override a specific property of a particular class to use more specialized rendering.

Templates in plugins are resolved as well. This means plugins such as Joda Time can provide default rendering for special property types. A template in your application will take precedence over a template in a plugin at the same 'level'. For example if a plugin provides a grails-app/views/_fields/string/_widget.gsp the same template in your application will override it but if the plugin provides grails-app/views/_fields/person/name/_widget.gsp it would be used in preference to the more general template in your application.

For most properties the out-of-the-box defaults should provide a good starting point.

Locating Templates Conventionally Example

Imagine an object of class Employee that extends the class Person and has a String name property.

You can override the template f:field uses with any of these:

  1. grails-app/views/controllerName/actionName/name/_wrapper.gsp
  2. grails-app/views/controllerName/actionName/string/_wrapper.gsp
  3. grails-app/views/controllerName/actionName/_wrapper.gsp
  4. grails-app/views/controllerName/name/_wrapper.gsp
  5. grails-app/views/controllerName/string/_wrapper.gsp
  6. grails-app/views/controllerName/_wrapper.gsp
  7. grails-app/views/_fields/employee/name/_wrapper.gsp
  8. grails-app/views/_fields/person/name/_wrapper.gsp
  9. grails-app/views/_fields/string/_wrapper.gsp
  10. grails-app/views/_fields/default/_wrapper.gsp

override the template f:widget uses with any of these:

  1. grails-app/views/controllerName/actionName/name/_widget.gsp
  2. grails-app/views/controllerName/actionName/string/_widget.gsp
  3. grails-app/views/controllerName/actionName/_widget.gsp
  4. grails-app/views/controllerName/name/_widget.gsp
  5. grails-app/views/controllerName/string/_widget.gsp
  6. grails-app/views/controllerName/_widget.gsp
  7. grails-app/views/_fields/employee/name/_widget.gsp
  8. grails-app/views/_fields/person/name/_widget.gsp
  9. grails-app/views/_fields/string/_widget.gsp
  10. grails-app/views/_fields/default/_widget.gsp

and override the template f:display uses with any of these:

  1. grails-app/views/controllerName/actionName/name/_displayWrapper.gsp
  2. grails-app/views/controllerName/actionName/string/_displayWrapper.gsp
  3. grails-app/views/controllerName/actionName/_displayWrapper.gsp
  4. grails-app/views/controllerName/name/_displayWrapper.gsp
  5. grails-app/views/controllerName/string/_displayWrapper.gsp
  6. grails-app/views/controllerName/_displayWrapper.gsp
  7. grails-app/views/_fields/employee/name/_displayWrapper.gsp
  8. grails-app/views/_fields/person/name/_displayWrapper.gsp
  9. grails-app/views/_fields/string/_displayWrapper.gsp
  10. grails-app/views/_fields/default/_displayWrapper.gsp

During template development it is usually recommended to disable template caching in order to allow the plugin to recognize new/renamed/moved templates without restarting the application. See the "Performance" section of the guide for the exact settings.

Default Behaviour - Using Grails Widget Tags

If no template override is found the plugin will use the standard grails input tags (e.g. g:select , g:checkbox , g:field ) for rendering input controls. Using f:field you can pass extra arguments (e.g. optionKey , optionValue ) through to these tags by prefixing them with widget-, e.g.

<f:field bean="person" property="gender" widget-optionValue="name"/>

Template parameters

The f:field and f:widget tags will pass the following parameters to your templates or to the body of f:field if you use one:

NameTypeDescription
beanObjectThe bean attribute as passed to the f:field or f:widget tag.
propertyStringThe property attribute as passed to the f:field or f:widget tag. This would generally be useful for the name attribute of a form input.
typeClassThe property type.
labelStringThe field label text. This is based on the label attribute passed to the f:field or f:widget tag. If no label attribute was used the label is resolved by convention - see below.
valueObjectthe property value. This can also be overridden or defaulted if the value or default attribute was passed to f:field or f:widget .
constraintsConstrainedPropertyThe constraints for the property if the bean is a domain or command object.
persistentPropertyGrailsDomainClassPropertyThe persistent property object if the bean is a domain object.
errorsList<String>The error messages for any field errors present on the property. If there are no errors this will be an empty List .
requiredboolean true if the field is required, i.e. has a nullable: false or blank: false constraint.
invalidboolean true if the property has any field errors.
prefixStringA string (including the trailing period) that should be appended before the input name such as name="${prefix}propertyName". The label is also modified.

In addition f:field passes the following parameters:

NameTypeDescription
widgetStringThe output of f:widget for the current bean and property if f:field was used without a tag body, otherwise the output of the tag body.

If the bean attribute was not supplied to f:field then bean , type , value and persistentProperty will all be null .

Field labels

If the label attribute is not supplied to the f:field tag then the label string passed to the field template is resolved by convention. The plugin uses the following order of preference for the label:

  1. An i18n message using the key ' beanClass . path .label'. For example when using <f:field bean="personInstance" property="address.city"/> the plugin will try the i18n key person.address.city.label. If the property path contains any index it is removed so <f:field bean="authorInstance" property="books0.title"/> would use the key author.books.title.label.
  2. An i18n message using the key ' objectType . propertyName .label'. For example when using <f:field bean="personInstance" property="address.city"/> the plugin will try the i18n key address.city.label.
  3. The natural property name. For example when using <f:field bean="personInstance" property="dateOfBirth"/> the plugin will use the label "Date Of Birth".

Locating Field Templates Directly

Rather than relying on the convention described previously to locate the template(s) to be used for a particular field, it is instead possible to directly specify the directory containing the templates. This feature was introduced in version 1.5.

  1. The wrapper attribute can be used with the f:field or f:display tags to specify the directory containing the _wrapper.gsp or _displayWrapper.gsp template to be used
  2. The widget attribute can be used with the f:field or f:display tags to specify the directory containing the _widget.gsp or _displayWidget.gsp template to be used
  3. If the wrapper and widget templates both have the same value, the templates attribute can be used instead as a shorthand. For example:

<f:field property="startDate" templates="bootstrap3" />

is equivalent to:

<f:field property="startDate" wrapper="bootstrap3" widget="bootstrap3" />

If a direct location is specified, and the templates cannot be found therein, the plugin will fall back to locating templates by convention.

Locating Templates Directly Example

// renders _fields/bootstrap3/_wrapper.gsp:
<f:field property="startDate" wrapper="bootstrap3"/>

// renders _fields/time/_widget.gsp: <f:field property="startDate" widget="time"/>

// renders _fields/time/_wrapper.gsp and _fields/time/_widget.gsp: <f:field property="startDate" templates="time"/>

// renders _fields/bootstrap3/_displayWrapper.gsp: <f:display property="startDate" wrapper="bootstrap3"/>

// renders _fields/time/_displayWidget.gsp: <f:display property="startDate" widget="time"/>

// renders _fields/time/_displayWrapper.gsp and _fields/time/_displayWidget.gsp: <f:display property="startDate" templates="time"/>