Creating Grails DRYer Development Forms

When using Grails, the GSP code to render each form field looks something like this:

<tr class="prop">
  <td valign="top" class="name"><label for="username">Login Name:</label></td>
  <td valign="top" class="value ${hasErrors(bean: person, field: 'username', 'errors')}">
    <input type="text" id="username" name="username" value="${person.username?.encodeAsHTML()}"/>
  </td>
</tr>

<tr class="prop">
  <td valign="top" class="name"><label for="userRealName">Full Name:</label></td>
  <td valign="top" class="value ${hasErrors(bean: person, field: 'userRealName', 'errors')}">
    <input type="text" id="userRealName" name="userRealName" value="${person.userRealName?.encodeAsHTML()}"/>
  </td>
</tr>

<tr class="prop">
  <td valign="top" class="name"><label for="passwd">Password:</label></td>
  <td valign="top" class="value ${hasErrors(bean: person, field: 'passwd', 'errors')}">
    <input type="password" id="passwd" name="passwd" value="${person.passwd?.encodeAsHTML()}"/>
  </td>
</tr>

Note that almost exactly the same 5 lines of GSP / HTML code are repeated for each form field. This does not seem very dry to me, and I wonder if others have found a better approach?

I found 2 plugins that are trying to solve this problem, a form helper and bean-fields . If anyone has experience using any of them, I would be very interested to hear from them. Alternatively, if there are other solutions / plugins, please let me know.

Thanks. Don

+5
source share
3

bean --. :

<bean:withBean beanName="person">
    <bean:field property="username" label="Login Name:"/>
    <bean:field property="userRealName" label="Full Name:"/>
    <bean:field property="passwd" label="Password:"/>
</bean:withBean>

DRYer?

+5

, - Grails 2.x Grails fields bean, bean

+8

Yes, the bean-fields plugin is very DRY ... your 20 lines can be replaced with one line:

<bean:form beanName="person" properties="username, userRealName, passwd"/>

(Suppose you have an i18n property set)

+5
source

All Articles