Grails IllegalStateException On CommandObjects

A very small but I’m sure useful snippet here for anyone banging their head against a wall trying to figure out why they cannot validate command objects. Or even why submitting a form they get this rather misleading exception message from Grails.

Method on class [com.tucanoo.SomeCommandObject] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly.

This happens when you are using CommandObjects behind your forms, rather than binding directly to Domain objects. Don’t be tempted to avoid Command Objects, they are worth it and help keep your application more secure and maintainable.

I don’t know why a Grails IllegalStateException exception is thrown, but it happens when you are validating (maybe not even explicitly) a command object that is not explicitly marked as Validateable.

Simply mark your class with the annotation @Validateable and you should find the problem goes away.

It’s very easy to forget NOT to mark command objects @Validateable, perhaps they should be by default as certainly for me, I like to supply a whole bunch of constraints with my command objects and ensure they validate before I let the data contained within going near my domain layer.

David Brown founded Tucanoo Solutions in 2011 and has written software in a professional capacity for over 30 years, back to the Java/J2EE days of Sun Microsystems. He builds cloud and web applications for clients in legal tech, government and beyond, including several years consulting to the United Nations on customs and transport systems. He writes these tutorials himself, with the full code on GitHub.

Leave a Reply

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