grails / grails-core

The Grails Web Application Framework
http://grails.org
Apache License 2.0
2.78k stars 950 forks source link

Grails 4.0.2 - BindingFormat annotation not binding date when using code for localized binding #11500

Closed manishkbharti closed 4 years ago

manishkbharti commented 4 years ago

Task List

Steps to Reproduce

  1. Clone the demo project - https://github.com/manishkbharti/bindingFormatIssueDemo
  2. Run the demo application
  3. Go to index action of test controller (http://localhost:8080/test/index)
  4. Click OK button
  5. You will get validation error (- grails.validation.ValidationErrors: 1 errors Field error in object 'com.mkb.DateCommand' on field 'aDate': rejected value [27/04/2019]; codes [com.mkb.DateCommand.aDate.typeMismatch.error,com.mkb.DateCommand.aDate.typeMismatch,dateCommand.aDate.typeMismatch.error,dateCommand.aDate.typeMismatch,typeMismatch.com.mkb.DateCommand.aDate,typeMismatch.aDate,typeMismatch.java.util.Date,typeMismatch]; arguments [aDate]; default message [Unparseable date: "27/04/2019"])

Expected Behaviour

Localized BindingFormat should give the expected date.

Actual Behaviour

Localized BindingFormat giving null.

Environment Information

Example Application

manishkbharti commented 4 years ago

Workaround --

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

and implements CO with grails.validation.Validateable trait.

niravassar commented 4 years ago

@puneetbehl I did some research on this, here is what i found

    protected String getFormatString(Annotation annotation) {
        assert annotation instanceof BindingFormat
        def code
        if(annotation instanceof BindingFormat) {
            code = ((BindingFormat)annotation).code()
        }
        def formatString
        if(code) {
            def locale = getLocale()
            formatString = messageSource.getMessage((String) code, [] as Object[], locale)
        }
        if(!formatString) {
            formatString = super.getFormatString(annotation)
        }
        formatString
    }

More notes 6/25

niravassar commented 4 years ago

Above i submitted a pr to fix this