Вопрос

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?

Это было полезно?

Решение

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
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top