(Quick Reference)

1. f:field

Description

f:field renders the widget using either f:widget or the tag body accompanied by any surrounding markup, typically a container, a label tag and any validation messages.

By default the f:field tag will output:

<div class="fieldcontain">
    <label for="foo">Foo</label>
    <!-- the widget as generated by f:widget or the tag body -->
</div>

The intention is that f:field should typically be used without a tag body. For example:

<f:field bean="person" property="name"/>

In which case the tag will use f:widget to generate an appropriate input. Alternatively in more specialized cases you can give f:field a tag body. For example:

<f:field bean="person" property="name">
    <g:textField name="${property}" value="${value}"/>
</f:field>

Since version 1.5 you can specify which specific templates are going to be used on the view. You can accomplish this using new attributes: wrapper, widget and templates. (See attributes section)

Since version 2.1.4 you can specify the theme to be used.

<f:field bean="person" property="name" theme="bs-horizontal"/>
// renders _fields/bootstrap3/_wrapper.gsp:
<f:field bean="person" property="name" wrapper="bootstrap3"/>

// renders _fields/maskedInput/_widget.gsp:
<f:field bean="person" property="name" widget="maskedInput"/>

// renders _fields/maskedInput/_wrapper.gsp and _fields/maskedInput/_widget.gsp:
<f:field bean="person" property="name" templates="maskedInput"/>

See Customizing Field Rendering for details of the parameters passed to the tag body.

The f:field tag handles embedded domain properties in a special way. See Embedded Properties for details.

Attributes
Table 1. Attributes for f:field
Name Required Description

bean

yes for f:widget if not inside f:with, optional for f:field

The bean whose property is being rendered. This can be the object itself or the name of a page-scope variable.

property

yes

The path to the property. This can include any level of nesting and numeric or string indexes. For example employees[0].address[home].street is a valid path. If absent, all properties will be displayed.

value

No

Overrides the actual value of the property.

default

No

A default value for the property that will be used if the actual property value is false.

required

No

Overrides the required status of the property. By default this is worked out based on the property’s constraints.

invalid

No

Overrides the validity of the property. By default this is worked out using the bean’s errors property for domain and command objects.

label

No

Overrides the field label passed to the template. This value may either be an i18n key or a literal string.

prefix

No

A string (including the trailing period) that should be appended before the input name such as name="${prefix}propertyName". The label is also modified.

wrapper

No

Specifies the name of the folder inside _fields where the _wrapper.gsp template is located.

widget

No

Specifies the name of the folder inside \_fields where the \_widget.gsp template is located.

templates

No

Specifies the name of the folder inside _fields where the _wrapper.gsp and _widget.gsp templates are located. It is a shorthand for specifying both (wrapper and widget).

theme

String

Theme to use if available.

Any additional attributes are passed to the rendered template. Any additional attributes prefixed with widget- are instead passed to the widget template or rendered on the default input.

Example of overriding a _wrapper.gsp

If you want to override a f:wrapper to be used for all widgets then create a file with content like this:

grails-app/views/_fields/default/_wrapper.gsp
<div class="form-group ${invalid ? 'has-error' : ''}">
    <label for="${property}">${label} ${required ? '*' : ''}</label>

    <div>
        <f:widget property="${property}"/>
        <g:if test="${errors}">
            <g:each in="${errors}" var="error">
                <span class="help-block"><g:message error="${error}"/></span>
            </g:each>
        </g:if>
    </div>
</div>