Grails: How to make everything I create Upper Case?

14,185

Solution 1

You could just write setters for your domain object?

class Domain {
 String aField

 void setAField( String s ){
   aField = s?.toUpperCase()
 }
}

Solution 2

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....

Solution 3

Using annotations is cleanest approach

import org.grails.databinding.BindingFormat
class Person {
    @BindingFormat('UPPERCASE')
    String someUpperCaseString

    @BindingFormat('LOWERCASE')
    String someLowerCaseString
}

Here is link for it: Grails doc for data binding

Solution 4

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)
            }
         }
      }
   }
Share:
14,185
randomizertech
Author by

randomizertech

All over the continent (from Greenland to Chile, and vice-versa)

Updated on June 28, 2022

Comments

  • randomizertech
    randomizertech almost 2 years

    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!

  • randomizertech
    randomizertech over 13 years
    Which one is shorter? Size is kind of an issue
  • hvgotcodes
    hvgotcodes over 13 years
    @fgualda87 -- i think tim_yates's solutions is better. and shorter
  • tim_yates
    tim_yates over 13 years
    @hvgotcodes ;) your way might be quicker if there's thousands of properties tho :) or maybe some sort of httpfilter
  • tim_yates
    tim_yates over 13 years
    @fgualda87 yeah, this method would require a setter for every uppercase prop
  • randomizertech
    randomizertech over 13 years
    yeah I only have 3 or 4 so I'll use your way, thanks to both!!
  • randomizertech
    randomizertech over 13 years
    The parameters inside the for should be change to the name of my domain right?
  • Dónal
    Dónal over 13 years
    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 grails.org/doc/1.3.5/api/org/codehaus/groovy/grails/commons/‌​…
  • dbrin
    dbrin over 12 years
    Thanks, Tim! Short and sweet.