Monday 18 February 2008

Grails: Tip for scaffolding required fields

Scaffolding is one of the most useful features of grails and customizing scaffolding it's basic for increase your productivity.

One thing I've customized is render a '*' for required fields, for this you have to install scaffolding templates with grails install-templates.

This is a fragment of the original templates/scaffolding/create.gsp,

props.each { p ->
if(p.type != Set.class) {
cp = domainClass.constrainedProperties[p.name]
display = (cp ? cp.display : true)
if(display) { %>
<tr class="prop">
<td valign="top" class="name">
<label for="${p.name}">${p.naturalName}:</label>
</td>
<td valign="top" class="value \${hasErrors(bean:${domainClass.propertyName},field:'${p.name}','errors')}">
${renderEditor(p)}
</td>
</tr>
<% } } } %>


I'm going to introduce a line, to check if the field has a constraint blank:false, and in this case render as a required field.

if(!cp?.blank) { %><span class="req">*</span><% } %>

The updated version complete:
   props.each { p ->
if(p.type != Set.class) {
cp = domainClass.constrainedProperties[p.name]
display = (cp ? cp.display : true)
if(display) { %>
<tr class="prop">
<td valign="top" class="name">
<% if(!cp?.blank) { %><span class="req">*</span><% } %>
<label for="${p.name}">${p.naturalName}:</label>
</td>
<td valign="top" class="value \${hasErrors(bean:${domainClass.propertyName},field:'${p.name}','errors')}">
${renderEditor(p)}
</td>
</tr>
<% } } } %>

That's all folks.

2 comments:

Anonymous said...

doubt your code will pass the turkey test!

Anonymous said...

mentioned that she newly learn an determiner about how pinpoint salons square measure pain since group square measure doing their have nails in guild to refrain money. So, I hypothesis when you think about it, $16 isn’t a harmful total for pass with flying colors meliorate you can get a decent sum of uses out of and have wittiness with it. I know it’s silly, but one of the pipe reasons I’ve avoided purchasing a join of fall boots is, well, that least rain down boots look like succession boots. Not to observe I detest lugging some a pair off of place to upshot into once I’m indoors. But in the end of a current pelter during my convert this morning, I decided that it’s finally time to obstruction being obstinate and to make being applied by investing in a twosome of synthetic rubber boots ulta coupons I didn't truly neediness to represent myself up for failure. Was stressful to embark on this year somaesthesia salty and bright, without all the required self-reproach that accompanies the first unsuccessful endeavour at quitting chocolate, sugar, alcohol...or whatever else I've definite to touch from my lifestyle.I'm particularly inclined of geologic process this manner of top in a glimmering colourize teamed up with a gladiator fabric, either somthing aeriform like material or head like chiffon. I also hump sex activity it with planted shave trousers for a flirty boylike energy. Here area unit trinity stylish slipway to include this top into your collection for some and all occasions Over the ending year this untidy equipment-style top broken pop up, and this is one perceptiveness I individual never fizzles out. It's the mint unbalance of looking at pulled together time at the Sami time beingness unbelievably comfortable. And it looks just as good alone as it does with quintuple pieces stratified on top.