Home Grails: How do a validate POST body consisting of a list of items using a command object?
Reply: 0

Grails: How do a validate POST body consisting of a list of items using a command object?

user7066 Published in September 19, 2018, 11:03 am

I am facing some issues with writing custom validators (Commands) in grails 3.3.3. Specifically, I am trying to validate POST request whose body is composed of a list of items. This is what I have...

The Command:

class VoteCommand implements Validateable {

    List<VoteItem> postList = [].withLazyDefault { new ListItem() }

    static constraints = {
        postList nullable: false

    class ListItem implements Validateable {
        String tag
        String some_id

        static constraints = {
            some_id nullable: false, blank: false
            tag nullable: false, blank: false
            tag inList: Tag.values() as List

AND the Payload:

    "noteVotesButWorks": [

                    "tag": "good"
                    "tag": "bad"

This payload passes the validation check in my controller action.

 def save(VoteCommand command) {

            println(command.errors) //grails.validation.ValidationErrors: 0 errors

            if (command.hasErrors()) {
                respond params.errors, view: 'create'
            } else {
                withFormat {
                    '*' { render status: CREATED }


After making the POST request to this action, I get a 201 and grails.validation.ValidationErrors: 0 errors printed to stdout.

Please, can someone here give me some pointers?

share|improve this question
  • 1
    This is not really related to your question but you have respond params.errors. Did you intend respond command.errors? – Jeff Scott Brown Apr 11 at 16:29
  • 1
    Also you have a withFormat block with only 1 case in it which is the '*' wildcard so withFormat isn't buying you anything. You could simply make the body of your else block to be render status: CREATED without the withFormat contraption and still have the exact same behavior. – Jeff Scott Brown Apr 11 at 16:30

2 Answers 2

active oldest votes
up vote 0 down vote
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.341489 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO