开发者

Grails: How to make everything I create Upper Case?

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!


You could just write setters for your domain object?

class Domain {
 String aField

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


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


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


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)
            }
         }
      }
   }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜