Making Grails form development DRYer
Asked Answered
H

3

5

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>

Notice that almost exactly the same 5 lines of GSP/HTML code is repeated for each form field. This doesn't seem very DRY to me, and I'm wondering if others have found a better approach?

I've found 2 plugins which attempt to address this problem, the form helper and bean-fields. If anyone has experience using either of these, I'd be very interested to hear from them. Alternatively, if there are other solutions/plugins, please let me know.

Thanks. Don

Hunley answered 25/1, 2010 at 14:10 Comment(0)
S
5

Using the bean-field plugin. Your code will become:

<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>

Can you find a DRYer solution?

Saddletree answered 25/1, 2010 at 19:1 Comment(1)
@sudhir's answer is DRYer, since the Grails Fields plugin will do all of this with a single tag.Hindmost
S
8

For those who read this thread in future - For grails 2.x branch Grails fields plugin is recommended over bean fields, its actually successor of bean-fields and provides flexibility to override default templates

Shorten answered 19/4, 2012 at 6:49 Comment(1)
I never realised I lived in the future until now :)Cryoscope
S
5

Using the bean-field plugin. Your code will become:

<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>

Can you find a DRYer solution?

Saddletree answered 25/1, 2010 at 19:1 Comment(1)
@sudhir's answer is DRYer, since the Grails Fields plugin will do all of this with a single tag.Hindmost
F
5

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

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

(Assuming you have i18n properties set)

Fallonfallout answered 25/1, 2010 at 19:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.