Reputation: 9296
I want to bind domain class with nested domain objects to request.JSON data. It works fine except for a field of type Date in nested domain class it always gives null. Here is request.json
[..., cardInfo:[expiryDate:2016-07-21, ccv:3455, cardNumber:4111111111111111], ....]
I tried to bind these json using multiple ways:
MyClass myClass=new MyClass(request.JSON)
And
myClass.properties=request.JSON
bindData(myClass,request.JSON)
bindData(myClass.cardInfo,request.JSON.cardInfo)
Nothing worked for binding expiryDate. Is this a bug or there is something wrong I'm doing?
UPDATE:
I'm using Grails 2.4.2 . I also have this line included in my config file
grails.databinding.dateFormats = ['dd-MM-yyyy','MM-dd-yyyy','yyyy-MM-dd', 'yyyy-MM-dd HH:mm:ss.S']
Upvotes: 0
Views: 2176
Reputation: 764
I think there are two problems here:
Upvotes: 0
Reputation: 27220
You haven't provided enough information to know for sure but I expect that the binder isn't configured to know about your date format. You can try something like this:
class MyClass {
@org.grails.databinding.BindingFormat('yyyy-MM-dd')
Date expiryDate
}
You can also configure that as a default date format in Config.groovy:
// grails-app/conf/Config.groovy
grails.databinding.dateFormats = ['yyyy-MM-dd', 'MMddyyyy', 'yyyy-MM-dd HH:mm:ss.S', "yyyy-MM-dd'T'hh:mm:ss'Z'"]
See http://grails.org/doc/latest/guide/theWebLayer.html#dataBinding for more details.
I hope that helps.
Upvotes: 1