Search code examples
validationgrailscommand-objects

Validate nested domain class instance in command object


I try to validate a nested domain class instance on a command object.

Having the following command object

package demo

import grails.databinding.BindingFormat

class SaveEventCommand {

    @BindingFormat('yyyy-MM-dd')
    Date date

    Refreshment refreshment

    static constraints = {
        date validator: { date -> date > new Date() + 3}
        refreshment nullable: true
    }
}

And having the following domain class with its own constraints

package demo

class Refreshment {

    String food
    String drink
    Integer quantity

    static constraints = {
        food inList: ['food1', 'food2', 'food3']
        drink nullable: true, inList: ['drink1', 'drink2', 'drink3']
        quantity: min: 1
    }
}

I need when refreshment is not nullable the command object validates the date property and check the corresponding restrictions in refreshment instance

For now try with this code in the controller:

def save(SaveEventCommand command) {
    if (command.hasErrors() || !command.refreshment.validate()) {
        respond ([errors: command.errors], view: 'create')

        return
    }

    // Store logic goes here
}

Here through !command.refreshment.validate() I try to validate the refresh instance but I get the result that there are no errors, even when passing data that is not correct.

Thank you any guide and thank you for your time


Solution

  • I typically just include some code that will use a custom validator to kick off validation for any property that is composed of another command object. For example:

    thePropertyInQuestion(nullable: true, validator: {val, obj, err ->
        if (val == null) return
        if (!val.validate()) {
            val.errors.allErrors.each { e ->
                err.rejectValue(
                    "thePropertyInQuestion.${e.arguments[0]}",
                    "${e.objectName}.${e.arguments[0]}.${e.code}",
                    e.arguments,
                    "${e.objectName}.${e.arguments[0]}.${e.code}"
                )
            }
        }
    })
    

    This way it's pretty clear that I want validation to occur. Plus it moves all the errors up into the root errors collection which makes things super easy for me.