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
Why we started using JSON Schema in our internal Helm charts
Helm 3 supports validating the provided chart values against a JSON Schema. While it may be quicker to get started in your chart development without a schema, we found it valuable for a number of […]
Rewriting files in Google Cloud Storage
Rewriting Files in GCP Note: even though this code is in Python, this should be the same idea in JavaScript, Go, etc. I wrote the following to copy a file from one Google Cloud Storage […]
Interpreting Spatial Data in the Age of COVID-19
As 2020 has come to an end, many are eager to leave the mess of COVID-19 behind with the new year and gain a fresh start. Unfortunately, new cases are still soaring across the United […]
Building a Better Mousetrap
Recently, my daughter (age 6) was into building “mousetraps” out of shoe boxes. These were more or less comfortable cardboard mouse houses complete with beds, rooms, everything a mouse could want or need and not […]