tags:

views:

93

answers:

1

In some grails code I've been reading, I see a function called "field()" that seems to do the following;

given an object of the form

def a = [a:b, c:d, e:123]

field(a) will produce the string

a="b" c="d" e="123"

i.e., it translates these name value pairs into a form sutiable for an html/xhtml/xml element.

I've searched the grails documentation for this function, and can't find it described anywhere. Where does it come from? Is it part of one of those other frameworks, like SiteMesh, that get magically included into Grails? (Though I can't find it in the SiteMesh docs, either!)

I tried it in my code, and it works, but it occasionally throws an exception and I want to see the real definition of this function and how it is supposed to be used.

+2  A: 

It's a part of Grails tag lib, located in this file:

src/java/org/codehaus/groovy/grails/plugins/web/taglib/FormTagLib.groovy

Here's its code:

/**
  * A general tag for creating fields
  */
def field = {attrs ->
    resolveAttributes(attrs)
    attrs.id = attrs.id ? attrs.id : attrs.name
    out << "<input type=\"${attrs.remove('type')}\" "
    outputAttributes(attrs)
    out << "/>"
}
chanwit
Thanks! (It's got a bug, too, if the value of a name/value pair is null, it throws an exception. It should really just output attribute="" )
ראובן
also interesting is it's not documented! opt/local/grails-1.2-M1/doc/api/org/codehaus/groovy/grails/plugins/web/taglib/FormTagLib.html doesn't mention it
ראובן
So, please file JIRA and submit a patch ;-)
chanwit