views:

62

answers:

3

Hello,

I am currently using CSS to change everything I write to upperCase when I create an entry, but that is not enough. When I save things, the text shown in the text fields is upper case, but the real value that Grails stores stays in lower case.

I am assuming I'd need to change something in the controller or anything.

Maybe transforming the $fieldValue CSS could work??

Any ideas would help!

Thnks!

+2  A: 

I think you are asking how to change values on your domain objects to uppercase. If this is not the case please clarify the question.

You have a bunch of options. I would recommend

1) In a service method, before you save, using String.toUpperCase() to modify the appropriate values on the domain object.

or

2) You can use the underlying Hibernate interceptors by defining a beforeInsert method on your domain object, and doing the toUpperCase there. (see 5.5.1 of the grails documentation)

or

3) You could do this client side. However, if it is a "business requirement" that the values are stored as upper, then I recommend doing the translation server side. It is easier to wrap tests around that code....

hvgotcodes
@hvgotcodes - Which one is shorter? Size is kind of an issue
fgualda87
@fgualda87 -- i think tim_yates's solutions is better. and shorter
hvgotcodes
+4  A: 

You could just write setters for your domain object?

class Domain {
 String aField

 void setAField( String s ){
   aField = s?.toUpperCase()
 }
}
tim_yates
@tim_yates lol, this is much simpler than what I came up with ;)
hvgotcodes
and I would need to do that with every String right??
fgualda87
@hvgotcodes ;) your way might be quicker if there's thousands of properties tho :) or maybe some sort of httpfilter
tim_yates
@fgualda87 yeah, this method would require a setter for every uppercase prop
tim_yates
yeah I only have 3 or 4 so I'll use your way, thanks to both!!
fgualda87
+1  A: 

You can use Groovy metaprogramming to change the setter for all domain class String-typed properties without actually writing a custom setter for each property.

To do this, add something like the following to the init closure of Bootstrap.groovy

   def init = { servletContext ->

      for (dc in grailsApplication.domainClasses) {        

         dc.class.metaClass.setProperty = { String name, value ->

            def metaProperty = delegate.class.metaClass.getMetaProperty(name)

            if (metaProperty) {

               // change the property value to uppercase if it's a String property
               if (value && metaProperty.type == String) {
                  value = value.toUpperCase()
               }

               metaProperty.setProperty(delegate, value)
            } else {
               throw new MissingPropertyException(name, delegate.class)
            }
         }
      }
   }
Don
The parameters inside the for should be change to the name of my domain right?
fgualda87
no, `dc` is just a local variable and `grailsApplication` is an implicit object that is available in `Bootstrap.groovy`, GSPs, and probably some other places too. I think it's an instance of http://www.grails.org/doc/1.3.5/api/org/codehaus/groovy/grails/commons/GrailsApplication.html
Don