External function definition to validate in Domain Class - grails

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
}

Related

Getting type of another generic type from Dart type parameter

I would like to make a generic class which only accepts Lists as a type parameter. But I also want the type parameter of the List. Something like this:
class MyClass<L extends List<T>> {
T foo() {
// ....
}
}
The problem is that that does not work. T is not found. But this does:
class MyClass<L extends List<T>, T> {
T foo() {
// ....
}
}
My only issue with this is that I have to always pass in the extra parameter T which should be inferred from the List type.
var instance = MyClass<List<int>>();
var instance = MyClass<List<int>, int>(); // Extra int kind of redundant
Is there any workaround to this?
The solution is similar to the one provided in this question (the same problem, but in Java): basically, you can't do that in Dart. What you can do is
create a new subclass:
class MyClass2<T> extends MyClass<List<T>, T> { ... }
or
create a factory method:
class MyClass<L extends List<T>, T> {
static MyClass<List<T>, T> fromList<T>(List<T> list) {
return MyClass(...);
}
}

Grails: How to mock a domain field validator?

Is there some way to mock a domain field validator?
Currently, my code in the domain class is looking like this:
isPrimary(validator: { Boolean value, Person obj ->
.......
}
And I need to mock this function.
I tried to use it like:
Person.metaClass.static.isPrimary.validator = { Boolean value, Person obj ->
.......
}
And it didn't work, any suggestions how to solve this issue ?
Here is an example:
class Person {
Boolean isPrimary
static constraints = {
isPrimary validator: isPrimaryValidator
//or this for a fully qualified validator
//isPrimary validator: Person.isPrimaryValidator
}
static isPrimaryValidator = { Boolean value, Person obj ->
//some validation
}
}
//in Test
Person.metaClass.'static'.isPrimaryValidator = { Boolean value, Person obj ->
//Do something else
}

grails 2.2.2 service don't know domain model: "No signature of method"

I have a grails-plugin called "listadmin" there is a domain model "Liste":
package listadmin
class Liste {
String internal_name
String public_name
Boolean edtiable = true
Boolean visible = true
static hasMany = [eintrage : ListenEintrag]
static constraints = {
internal_name(unique : true , blank : false);
}
String toString() {
"${public_name}"
}
}
I have service called "SECO_ListenService" in the same module (grails-plugin):
package listadmin
class SECO_ListenService {
def getEntriesOfList(String intnalListName) {
def aList = Liste.findByInternal_name(intnalListName)
return aList
}
}
Now I try to call this service from an other module (grails-plugin) called "institutionadmin". The SECO_ListenService should return a list of strings for an select of a domain model in the inistitutionadmin:
package institutionadmin
import listadmin.SECO_ListenService
class Einrichtung {
Long einrichtungs_type
Long type_of_conzept
int anzahl_gruppen
int anzahl_kinder_pro_Gruppe
String offnungszeiten
static hasMany = [rooms : Raum]
static constraints = {
def aList = []
def sECO_ListenService = new SECO_ListenService()
aList=sECO_ListenService.getEntriesOfList("einrichtung_type")
einrichtungs_type(inList: aList)
}
}
If I try to run this application with the both modules. I get the following error:
Caused by MissingMethodException: No signature of method:
listadmin.Liste.methodMissing() is applicable for argument types: ()
values: []
It seemed to be that the service class don't know the "Liste"-domain-model. But I don't know where the error is. I also tried to call other standard methods like "findAll" but without any success.
Has anybody an idea where my mistake could be?
To get a service in a static context you need to access the grailsApplication spring bean. This can be done thought Holders. Example:
class MyService {
List<String> getAvailable() {
return ['A','B','C']
}
}
class MyDomainClass {
String something
static constraints = {
something inList: getSomethingList()
}
static List<String> getSomethingList() {
def myService = Holders.grailsApplication.mainContext.getBean('myService')
return myService.getAvailable()
}
}

In Grails, how do I apply an inList constraint from a 3rd party data source?

Let's say I have the following model:
class Product {
String name
String price
String currency
static constraints = {
currency inList: ['USD', 'EUR']
}
Now we have a new requirement that inList constraint for currency must be pulled from a service:
class CurrencyService {
def getAvailableCurrencies = {
...
}
}
How do I make this work? I tried:
class Product {
def currencyService
...
static constraints = {
currency inList: currencyService.getAvailableCurrencies()
}
}
But I can't access the currencyService instance in the static constraints context. I also tried using static currencyService, but this likewise does not work. Any ideas?
As dmahapatro mentioned above, you could use a custom validator which uses your service:
static constraints = {
currency validator: { value, obj ->
if (!(value in obj.currencyService.getAvailableCurrencies()))
return ['invalid.currency']
}
}

Grails validation dependent on other attributes

What is the correct way to do something like this with grails:
class myDomainThing {
String description
MyOtherDomainThing otherThing
static constraints = {
description(nullable:if(otherThing))
otherThing(nullable:if(description))
}
}
So I either want there to be a link to the otherDomainThing or I want a String description.
You will have to use Grails custom validation using the
validator
static constraints = {
description(validator: {
return otherThing and !description
})
}
you'll need to use a custom validator
static constraints = {
description validator: { val, obj ->
if(otherthing && val) {
false
}
else {
true
}
}
}
obviously some pseudocode in there around otherthing

Resources