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.

Founder of Tucanoo Solutions Ltd, a Cloud / Web Application development company. AWS Cloud Solutions Architect. Specialties: Spring Boot, Java, Grails, React.JS, App Architecture, Agile, Scrum, Git, AWS, Javascript.

Leave a Reply

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