4

I'm only getting the default validator message. What am I doing wrong?

class Questao {


static hasMany = [alternativas:Alternativa]

static constraints = {

    alternativas (validator: {val, obj ->
       if(val.size() < 2)
            return ['validator.message'] //custom message
        })
}
}

/i18n

questao.alternativas.validator.message = "must be greater than two"

default.invalid.validator.message= Property [{0}] of class [{1}] with value [{2}] does not pass custom validation

Thanks

Luccas
  • 4,078
  • 6
  • 42
  • 72

3 Answers3

7

You're returning a list containing your message code - you need to return just the code:

alternativas validator: { val, obj ->
   if (val.size() < 2) {
      return 'validator.message' //custom message
   }
}
Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
3

This API has changed. Returning a string with a custom error message will no longer work. Unfortunately, this is not well-documented.

Returning a string or a non-empty list will evaluate to true according to the Groovy Truth. Which means, the validator signals, "accepted". - Either return false, or, if you need a custom error message, explicitly rejectValue(..) the value:

alternativas (validator: {val, obj ->
   if(val.size() < 2)
       obj.errors.rejectValue('alternatives',
           'questao.alternativas.validator.message')
    })
robbbert
  • 2,183
  • 15
  • 15
  • It's not documented because it's not true. – Burt Beckwith Nov 07 '10 at 04:52
  • Sorry, I was mistaken about that. - However, returning a string from a `validator` is not documented, at all, in neither the Grails *User Guide* nor the *Wiki* (in the latter, not at a prominent place, at least). - Thus, it's quite likely to draw wrong conclusions when simple test cases on that issue fail. – robbbert Nov 07 '10 at 20:29
  • It is documented, though: http://grails.org/doc/latest/ref/Constraints/validator.html - If you look in the list at the bottom, it says `The closure can return ... a string to indicate the error code to append to the "classname.propertName." ...` The toughest part about it all is understanding how the returned string maps to a key in `messages.properties`; once you get the hang of that, though, it's not too tricky. – Rob Hruska Nov 08 '10 at 17:57
  • what about the part about returning a list in the doc `otherProperty validator: { return ['custom.error', arg1, arg2] }` is not really well documented – user2427 Oct 25 '12 at 20:34
  • Not really well documented is an euphemism. It sucks big time! – Igor Donin Aug 19 '15 at 15:02
  • >Returning a string or a non-empty list will evaluate to true according to the Groovy Truth I was suffering from the same issue and indeed it works this way if third parameter (Errors object) is passed to validator. After removing third parameter it started working as expected, i.e. returning error code string or list with error code was correctly indicated as an error. – Jacek Sep 15 '15 at 08:14
0

Additionally:

In Grails 2.5.2 (I only tested in this version) if you use the errors argument in the custom validator closure and return a message code (or an array with the message code) it does not work, you need to use directly the errors object to put the message code.

iberck
  • 2,372
  • 5
  • 31
  • 41