简体   繁体   中英

External function definition to validate in Domain Class

My validation looks like:

static constraints =
{
   someProperty validator: { val, obj ->

      // a lot of code here

   }
}

How can I define external function which will pass to this validation (val, obj requierd) ? Now my code isn't clear in constraints closures... there's too much validation code for someProperty. How can I change it?

By creating a groovy class in the src/groovy directory, like :

public class CustomValidators {
    static validateMe = { val, obj ->
        // a dummy example...
        return val < 1
    }
}

Then, on your domain class use it like below :

static constraints =
{
    someProperty validator: CustomValidators.validateMe
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM