1

I try to validate two language fields from two different objekts. I found Grails Validation and so i created:

class Test {
Title title
Summary summary

static contraints ={
      title validator: { val, obj ->
      if (obj.title.getLanguage().equals(obj.summary.getLanguage())) println "same language"
    }
  }
}

domain Summary

class Summary{
String language }

and domain Title

class Title{
String language}

But it seems to me that the validator doesnt react. Thanks in advance.

Eddy2Go
  • 19
  • 3
  • can you build a simple app with just the problematic code and post it on github, so we can help you debug? – Raphael Jun 08 '14 at 13:50
  • I uploaded my example on git: https://github.com/Spooney/GrailsTest.git Thanks in advance – Eddy2Go Jun 15 '14 at 14:33

2 Answers2

0

Without seeing the rest of your Domain I can't be sure of the specifics but are missing something important. You are missing the reference to the instance being validated. When using custom validation there are two optional parameters being passed into your validation closure. One is the instance of the class being validated, the other is the value of the current property. So, given a domain such as this:

class MyPerson {
  String name
  String nickName

  static constraints = {
    name(nullable: false, blank: false)
    nickName(validator: {val, obj ->
      if (obj.name == val) return false // ensure the name and nickname do not equal one another
      return true
    })
  }
}

So in the above example the custom validation ensures that the name and nick name do not match one another. Notice the use of obj.name to reference another property of the instance. This is key to what you are attempting to do.

In your case your validation may look something like this:

static contraints ={
  title validator: { val, obj ->
    if (obj.title.getLanguage().equals(obj.summary.getLanguage())) println "same language"          
  }       
}

Hope this helps.

Joshua Moore
  • 24,706
  • 6
  • 50
  • 73
  • Thank you for your reply but that didnt work either. I updated my question with the domain classes. – Eddy2Go Jun 07 '14 at 10:02
  • You aren't passing the parameters into your validator as you should. See how you need to use `validator: { val, obj ->` Otherwise obj has no meaning. – Joshua Moore Jun 07 '14 at 10:05
  • I tried your new version. Now i get an internal server error. – Eddy2Go Jun 15 '14 at 14:02
0

Try this code

static constraints = {    
  title blank: false, nullable: false,validator:{ value , object -> 
             //get title object from database,based on your logic/requirement  
                 def title =  Title
             // get summary object from database,based on your logic/requirement 
                 def summary =  Summary          
                if (title.getLanguage().equals(summary.getLanguage()))
                     println "same language"          
                     return true
                } else {
                       //show message for validation failed.
                    return ['not.unique.validation','Langauge']
                }
            }
}
Anand Kushwaha
  • 457
  • 4
  • 15