Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making Grails form development DRYer

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

like image 541
Dónal Avatar asked Jan 25 '10 14:01

Dónal


1 Answers

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

like image 198
Sudhir N Avatar answered Sep 19 '22 11:09

Sudhir N