MKB
MKB

Reputation: 7619

grails 4 - BindingFormat annotation not binding date when using code for localized binding

As per the docs, we can use localized data binding. But this is not working in grails 4.

controller

import grails.databinding.BindingFormat

class TestController {

  def index() {
    render view: 'index'
  }

  def test(DateCommand command) {
    render "${params} <br/><br/> - ${command.errors?.collect { error -> error as String }?.join(', ')}"
  }
}

class DateCommand {

  @BindingFormat(code = 'date.field.format')
  Date aDate
  @BindingFormat('dd/MM/yyyy')
  Date bDate

  static constraints = {
    aDate(nullable: false)
    bDate(nullable: false)
  }
}

index view

<g:form action="test">
  <input type="text" name="aDate" value="27/04/2019" />
  <input type="text" name="bDate" value="27/04/2019" />
  <g:submitButton class="btn btn-success" name="OK" />
</g:form>

messages.properties

date.field.format=dd/MM/yyyy

Same code is working fine in grails 3.x.x

Am I missing some configuration or something is wrong in the code?

Upvotes: 1

Views: 313

Answers (2)

MKB
MKB

Reputation: 7619

Workaround --

command.aDate = params.date('aDate', message(code: 'date.field.format'))
command.clearErrors()
command.validate()

and implements CO with grails.validation.Validateable trait.

Upvotes: 0

Jeff Scott Brown
Jeff Scott Brown

Reputation: 27255

Am I missing some configuration or something is wrong in the code?

It looks like the annotation handling may be broken for that. If you file an issue at https://github.com/grails/grails-core/issues we can get it straightened out.

Thanks for the feedback.

Upvotes: 2

Related Questions