<div class="fieldcontain">
<label for="foo">Foo</label>
<!-- the widget as generated by f:widget or the tag body -->
</div>
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:
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
Name | Required | Description |
---|---|---|
|
yes for |
The bean whose property is being rendered. This can be the object itself or the name of a page-scope variable. |
|
yes |
The path to the property. This can include any level of nesting and numeric or string indexes. For example |
|
No |
Overrides the actual value of the property. |
|
No |
A default value for the property that will be used if the actual property value is |
|
No |
Overrides the required status of the property. By default this is worked out based on the property’s constraints. |
|
No |
Overrides the validity of the property. By default this is worked out using the bean’s errors property for domain and command objects. |
|
No |
Overrides the field label passed to the template. This value may either be an i18n key or a literal string. |
|
No |
A string (including the trailing period) that should be appended before the input name such as |
|
No |
Specifies the name of the folder inside |
|
No |
Specifies the name of the folder inside |
|
No |
Specifies the name of the folder inside |
|
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:
<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>