0
votes

I am trying to implement some command object validation, but one property of command object is not binding its always return null

Domain class

package ni.sb

class PurchaseOrder implements Serializable {
  Date dateCreated
  Date dutyDate
  String invoiceNumber
  BigDecimal balance
  String typeOfPurchase

  Date lastUpdated

  static constraints = {
    dutyDate nullable:false, validator: { dutyDate ->
    def today = new Date()

    if (dutyDate <= today) {
      "notMatch"
    }
   }
   invoiceNumber blank:false, unique:true
   balance nullable:true
   typeOfPurchase inList:["Contado", "Credito"], maxSize:255
 }

}

This is the command object

class PurchaseOrderCommand implements Serializable {
  Date dutyDate
  String invoiceNumber
  String typeOfPurchase

  static constraints = {
   importFrom PurchaseOrder
 }

}

Here is the controller action

def actName(PurchaseOrderCommand cmd) {
  if (cmd.hasErrors()) {
    println params.dump()
    println cmd.dump()

    return
  }
}

dutyDate is not binding, after i try dumb() in params and cmd i get this

snippet params.dump()

dutyDate:2014-09-25

snippet cmd.dump()

dutyDate=null

I hope you can help me

1
What version of Grails are you using? - Joshua Moore

1 Answers

0
votes

If you inspect cmd.errors I expect you will see the error there.

If your date request parameters are formatted like "2014-09-25" and you are using a recent version of Grails then something like this should work:

import org.grails.databinding.BindingFormat

class PurchaseOrderCommand implements Serializable {
    @BindingFormat('yyyy-MM-dd')
    Date dutyDate
    String invoiceNumber
    String typeOfPurchase
}

Alternatively you could set "yyyy-MM-dd" as one of the default formats 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'"]
// ...

I hope that helps.