Mar 11, 2015

Easily get GORM max size constraints

Oftentimes when designing a UI and working with field limits (such as an HTML input’s “maxlength” attribute), I’ve found myself wondering why it’s not more common to have the constraint come directly from the server. The frequent scenario seems to be that the database has one constraint for the field, and the UI has another — even if they happen to match.

For example:

Shouldn’t the UI limit come from the object’s field constraint? It would certainly make it easier to maintain should it ever change; plus it’s more clear on the UI where the value comes from. Grails has a great ValidationTagLib to report errors after-the-fact, but as far as I’m aware, there’s no easy way to get these constraints beforehand.

I decided to take a stab at creating a taglib that will do just this. It should hopefully be extendable to other fields in the future (such as “nullable” or “blank”), but this should be a good start. First let’s introduce our sample GORM object:

Note that it utilizes both the “maxSize” and the “size” constraints (sometimes both, for testing purposes). Other fields do not have any constraints. Ideally we would want the smallest valid value to be returned, otherwise some flag (null/empty string) when the field does not have such constraints. So let’s take a look at a potential implementation:

Walking through the code we see that an exception is raised when the proper parameters aren’t passed, or when the specified class or field do not exist. If the field does exist, we will take the lesser of “maxSize” and “size”, and return an empty string if no such constraint exists. I chose to use Class.forName() and specify the ClassLoader (rather than the simpler grailsApplication.getClassForName() approach) as it worked better for my tests.

And of course, some tests to illustrate the different scenarios:

Now that we have the taglib done, we can invoke the “limit” method with:

A bit more code than the alternative, but hopefully a lot more maintainable as well. I’ve posted the taglib up on GitHub in case I (or anyone else!) want to extend the functionality in the future. Hope this helps!

Igor Shults

About the Author

Object Partners profile.

One thought on “Easily get GORM max size constraints

  1. Clessio Mendes says:

    Well done. I’m borrowing your approach.

    1. Clessio Mendes says:

      Well done. I’m borrowing your approach. However, have you considered that the field size could be set as a mapping definition, or even could receive a default 255 size when not explicitly defined? In both cases, getting the actual field size from the inner database engine seems a even better approach.

      static mapping = {
      username length: 50
      firstname defaultValue: “john” //sets size as 255 by default
      }

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
Design Systems, Part 1 • Introduction
Business leaders need a practical guide to plan and execute Design System Initiatives. The aim of this series is to be that guide. This installment introduces terms and definitions as a primer on Design Systems.
ML for Translating Dysarthria Speech (Pre-Part 1)
What is Dysarthria? Per the Mayo Clinic, Dysarthria occurs when the muscles you use for speech are weak or you have difficulty controlling them. Dysarthria often causes slurred or slow speech that can be difficult […]
Develop Quality Code
As software continues to dominate every facet of our lives, developers are faced with an ever-increasing pressure to produce bug free code. The responsibility of clean quality software falls upon everyone that is involved in […]
Clean JSX structuring in React
A video guide on how to structure your JSX to make it easier to know when it’s appropriate to split out new components. This will help keep your React code smaller, cleaner, and more DRY.