You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@struts.apache.org by "Jose L Martinez-Avial (JIRA)" <ji...@apache.org> on 2013/12/20 17:00:12 UTC

[jira] [Closed] (WW-4208) Allow customization of validation messages by passing arguments to the validator for the message

     [ https://issues.apache.org/jira/browse/WW-4208?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Jose L Martinez-Avial closed WW-4208.
-------------------------------------

    Resolution: Fixed

The same effect can be obtained using the already existing functionality. I can create the following annotation to validate a field:

@RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "phoneCountryCode",
                    messageParams={"getText('PhoneCountryCode.label')"},key = "validation-error.mandatory")},

And then define the following i18n properties
PhoneCountryCode.label       = Country Code

validation-error.mandatory     = The field {0} is mandatory

And then the generated message will be "The field Country Code is mandatory"

> Allow customization of validation messages by passing arguments to the validator for the message
> ------------------------------------------------------------------------------------------------
>
>                 Key: WW-4208
>                 URL: https://issues.apache.org/jira/browse/WW-4208
>             Project: Struts 2
>          Issue Type: Improvement
>          Components: Annotations, XML Validators
>    Affects Versions: 2.3.15.2
>            Reporter: Jose L Martinez-Avial
>              Labels: annotation, validation
>             Fix For: 2.3.17
>
>
> As of today the key/messages used by the annotated validators do not allow to receive custom arguments, since only the parameters defined by the annotation can be used. In order to improve that I suggest to add a parameter keyArgs to the validation annotations so it will be passed to the OGNL evaluator when it evaluates the key provided in the annotation. That way the following would be possible:
> {code}
> @Validations(
>         requiredStrings =
>             {@RequiredStringValidator(type = ValidatorType.SIMPLE,
>                     fieldName = "event.email", key = "validation-error.mandatory")
>                     keyArgs={"email","one","two"}
>         }
> )
> {code}
> Then the key/message could be something like this:
> {code}
> validation-error.mandatory = The field ${getText(keyArgs[0])} is mandatory
> email= E-mail
> {code}
> And the result would be
> {code}
> The field E-mail is mandatory
> {code}
> It would allow to customize the message even further than it allows now. I suppose it would need to be implemented also in xml validations, but I haven't used them, so I don't what would be needed for that. 



--
This message was sent by Atlassian JIRA
(v6.1.4#6159)