command objects - Grails 2.5.0 - constraint be blank OR follow validation -


i want like:

class mycommand {      string name      string data       static constraints = {          name blank: false, size: 3..64          data (blank: true) || (blank: false, size: 3..64)      } } 

where data either blank or follows validation such size constraint if not blank. possible without custom validation?

it non-trivial use other constraints within validator constraint. delegate of constraints closure constrainedpropertybuilder, can read understand complexity.

but doesn't matter because emailconstraint uses apache's emailvalidator, can use in validator. here's emailvalidator in action:

@grab('commons-validator:commons-validator:1.4.1')  import org.apache.commons.validator.routines.emailvalidator  def emailvalidator = emailvalidator.getinstance();  assert emailvalidator.isvalid('what.a.shame@us.elections.gov') assert !emailvalidator.isvalid('an_invalid_emai_address') 

you can use emailvalidator in own validator this:

import org.apache.commons.validator.routines.emailvalidator  class mycommand {      string name      string data       static constraints = {          name blank: false, size: 3..64          data validator: {              if(it) {                  emailvalidator                      .getinstance()                      .isvalid(it)              } else { true }          }      } } 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -