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
SwiftGen with Image & Color Asset Catalogs
You might remember back in 2015 when iOS 9 was introduced, and we were finally given a way to manage all of our assets in one place with Asset Catalogs. A few years later, support […]
Managing your Helm deployments with Helmfile
As we’ve been using Kubernetes to build software delivery platforms for our clients, we’ve found Helm to be a reasonable solution to templating and managing deployments to Kubernetes.  Although templating alternatives such as Kustomize have […]
AWS CodeBuild Test Reports for Gradle builds
Although AWS documentation has instructions for adding Test Reports for a maven build they currently lack instructions for a gradle build. You can find the maven instructions here: https://aws.amazon.com/blogs/devops/test-reports-with-aws-codebuild/ Assuming you have your gradle wrapper […]
Structuring SwiftUI Previews for API Calls
SwiftUI, together with Combine and Xcode 11+, provide a powerful toolset for quickly creating an app with a native UI. In Xcode 11+, the preview pane was introduced in order to provide live snapshots of […]