0

I have a user registration form where the fields are validated using command object. One of the fields is a checkbox, which must checked before proceeding the registration, and it isn't saved to the domain object. This checkbox has a corresponding Boolean field in the command object. When checkbox is not checked, a validation error is thrown from a custom validator.

The problem is, that this error is not propagated in the <g:renderErrors bean="${command}" as="xml"/> block (the validator is fired correctly).

The command object:

class RegisterCommand {

...
Boolean termsChecked
...
static constraints = {
    ...
    termsChecked validator: RegisterController.termsCheckedValidator
}

Validator:

static final termsCheckedValidator = {termsChecked, command, errors ->
    if (!command.termsChecked) {
        return 'registerCommand.termsChecked.required'
    }
}

Checkbox in the GSP file:

<g:checkBox value="${command.termsChecked}" bean="${command}" name='termsChecked'/>

How this could be solved?

rpozarickij
  • 1,477
  • 3
  • 14
  • 27

1 Answers1

3

If you pass 3 parameters to the validator with the last one being errors then the return code is ignored thinking that the Spring Errors is taking care of errors.

If you want to use the error code then just pass 2 parameter to the validator as

static final termsCheckedValidator = {termsChecked, command ->
    if (!command.termsChecked) {
        return ['required.termsChecked']
    }
}


//messages.properties
registerCommand.termsChecked.required.termsChecked=blah
dmahapatro
  • 49,365
  • 7
  • 88
  • 117
  • Thanks for your response. I removed the `errors` variable, and now after form submission I'm getting the following message: `URI /projectName/register/register Class groovy.lang.MissingMethodException Message No signature of method: static org.apache.commons.lang.StringEscapeUtils.escapeXml() is applicable for argument types: (java.lang.Boolean) values: [false] Possible solutions: escapeXml(java.lang.String), escapeXml(java.io.Writer, java.lang.String), escapeHtml(java.lang.String), escapeSql(java.lang.String)` Edit: getting errors in `list` format works fine, but i need them in xml. – rpozarickij Oct 10 '13 at 15:38
  • @cNduo Try a different variety of message key. Also note to return as `list`. See my update. – dmahapatro Oct 10 '13 at 16:14
  • Your update indeed solves the problem, but only if the error output format is `list`. What about the `xml` format? Because I'm still getting the same error as in my first comment. – rpozarickij Oct 11 '13 at 06:25