grails 4 - 使用代码进行本地化绑定时,BindingFormat 注释未绑定日期

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

根据 docs,我们可以使用本地化数据绑定。但这在 grails 4 中不起作用。

控制器

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)
  }
}

索引视图

<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

相同的代码在 grails 中运行良好3.x.x

是我遗漏了一些配置还是代码有问题?

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

看起来注解处理可能为此被破坏了。如果您在 https://github.com/grails/grails-core/issues 提出问题,我们可以解决问题。

感谢反馈。

解决方法 --

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

并实施具有 grails.validation.Validateable 特征的 CO。