开发者

Grails - Command object, customized conditional validator

I would like to build a customized validator in my Command object, to make sure the field email address of a form will not be empty if the notifyMe checkbox is selected.

Here is my latest attempt to implement it:

email(blank: true, nullable: true, email: true,
                  validator: {email, creditProviderCommand ->
                              if (creditProviderCommand.notifyMe == 'on')
                                    return email.size() > 0})

I have tried also with email != null and email != '' but it is not working in both cases, meaning that the form submission is accepted even with the notifyMe checkbox checked and the email addre开发者_Go百科ss field left blank.

The code of the action handles the validation error properly (even unique constraint). Any idea of what I am doing wrong ?

Thank you very much for your help.


The code above looks fine to me. Are you 100% sure that creditProviderCommand.notifyMe == 'on' when the checkbox is checked?

the form submission is accepted even with the notifyMe checkbox

The form submission will always be accepted, even when there are validation errors. It is your responsibility to check for validation errors and decide what to do when validation fails, e.g.

def myAction = {MyCommand cmd ->

  if (cmd.validate()) {
    // code to be executed when validation succeeds
  } else {
    // code to be executed when validation fails
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜