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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With