Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-6730

Access JSR-303 validation contraint attributes in localized messages

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: 3.0 GA
    • Fix Version/s: 3.0.4
    • Component/s: Core, Web
    • Labels:
      None

      Description

      Related to: SPR-6407 and SPR-5418.

      When using declarative validation constraints with Spring, custom/localized messages should be able to access the arguments to the validation annotation in a consistent manner that is guaranteed to work across validator providers.

      For example, when applying an annotation like "@Size(min=1, max=25)" to a field you should be able to provide an error message format like "Size.fieldName=Size of

      {0}

      must be between

      {1}

      and

      {2}

      ".

      Currently, the values of the arguments passed to the annotation are available to the message source in an arguments array, but the position of each value in the array is not consistent. It appears that the positions of the arguments in the array can change depending on the validator provider and potentially change between releases of a validator provider. If the framework does currently provide some guarantee of the order, then this should be documented.

      Here are some examples of validator annotations and the arguments array that is available to the message source when the validation fails. In all cases the first ([0]) element in the array is a DefaultMessageSourceResolvable containing the name of the field in error.

      @Size(min=1, max=25)

      index contents
      1 "message" argument from annotation
      2 "min" argument from annotation
      3 "payload" argument from annotation
      4 "max" attribute of annotation
      5 "groups" attribute of annotation

      @Max(25)

      index contents
      1 "value" argument from annotation
      2 "message" argument from annotation
      3 "groups" attribute of annotation
      4 "payload" argument from annotation

      @Min(1)

      index contents
      1 "value" argument from annotation
      2 "message" argument from annotation
      3 "groups" attribute of annotation
      4 "payload" argument from annotation

      @Pattern(regexp="[A-Z]+")

      index contents
      1 "message" argument from annotation
      2 "payload" argument from annotation
      3 "flags" argument from annotation
      4 "regexp" attribute of annotation
      5 "groups" attribute of annotation

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                juergen.hoeller Juergen Hoeller
                Reporter:
                scottyfred Scott Frederick
                Last updater:
                Janning Vygen
              • Votes:
                3 Vote for this issue
                Watchers:
                9 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Days since last comment:
                  3 years, 38 weeks, 6 days ago