1

How can I access the pre-built constraint validations in Grails?

Lets say, for example, I want to use the range validator on a Map Object

Map<String, Double> myData

static constraints = {
   myData validator: { val, obj, errors ->
       //Iterate through all Double values and validate using `range` constraint
   }
}

It doesn't seem to make sense to have to write my own range validation method since Grails already has one built-in, somewhere. How can I access it?

Just to clarify, range constraint is just an example. Just looking for how to access any of those pre-builts in general.

Update:

To clarify with a simpler example. I know how to use the constraints in the standard way. I am trying to use the constraint validations programmatically which I cannot find documentation for.

def myMethod() {
    int myInt = 5;

    //HOW DO I CALL RANGE (OR ANY) OF THE PRE-BUILT VALIDATION METHODS
    if(RANGE(myInt, 0..5)) {
        //DO SOMETHING
    }
}

Like i said, I don't want to reinvent the wheel if it's already there. The API that the validation constraints use must be somewhere, how can I manually call those API's?

Raymond Holguin
  • 1,050
  • 2
  • 12
  • 35
  • http://docs.grails.org/3.2.7/ref/Constraints/Usage.html listed here on RHS quick reference bar – V H Aug 07 '18 at 12:49
  • did domainInstance.validate() not work? – elixir Aug 07 '18 at 21:23
  • @Vahid Sorry for not being clear. I know how to use constraints as per the docs. I am looking to use the validation methods outside the constraints. Please see updated question. – Raymond Holguin Aug 10 '18 at 22:17
  • I don't think you can do what you are trying to do Grails validation simply extends java validation and in short if you can do it in Java then so should it be possible in groovy. https://stackoverflow.com/questions/28702809/how-to-manually-trigger-spring-validation. Basically what your best bet I think for your scenario is to create a Validation bean like https://github.com/vahidhedayati/grails-queuemail-plugin/blob/master/src/main/groovy/org/grails/plugin/queuemail/validation/EmailQueueBean.groovy. Then let the bean call the validation methods like bean.validate() in your method – V H Aug 13 '18 at 08:07
  • Sorry I hope it don't confuse you even more, so typically you would bind the bean with the data before passing into service https://github.com/vahidhedayati/grails-queuemail-plugin/blob/master/grails-app/services/org/grails/plugin/queuemail/QueueMailApiService.groovy#L243 - you could do all its validation in the controller - the controller auto magically binds received params to a validation bean - you would need to follow some of my controllers in the most recent plugins to see how it works - or in the service `MyBean bean = new MyBean(); bean.myInt=5; if (bean.validate()) { //do something}` – V H Aug 13 '18 at 08:18

1 Answers1

0

myDate range: 18..65 //could be age of a person for example

Taken from: http://docs.grails.org/3.2.7/ref/Constraints/range.html

Tuomas Valtonen
  • 488
  • 4
  • 13
  • Sorry that is not what I'm asking. I know how to use constraints as per the docs. Please see updated question. – Raymond Holguin Aug 10 '18 at 22:17
  • That is Spring under the hood so it’s validation framework should give you this functionality. When it comes to grails, i don’t know how to use utilize it in the way you need. – Tuomas Valtonen Aug 16 '18 at 07:53