Yes, what I would like is to define a message for each validator that could
be reused with any field just by having the fieldname translated. Let's say
I have a form to create an user, with fields First name, Last name and
Email, where all the fields are required. I would define the following
properties for the labels of the fields

firstName = First name
lastName = Last name
email = E-mail

The field is going to be mapped to a bean "user" in the action. In that
case, the messages used for the validation would need to be as follows:

validation-error.mandatory        = The field ${getText(fieldName)} is
mandatory
user.firstName = First name
user.lastName = Last name
user.email = E-mail

So I need to duplicate the messages. If I have another form, let's say
person, with the same fields, I would also need to create

person.firstName = First name
person.lastName = Last name
person..email = E-mail

I have to duplicate the message keys for any bean name I want to use, since
they can not be reused. It complicates the maintenance of the code. and the
reason for this is that there is no other way to pass an argument to the
key (which is actually an OGNL expression) that to use an attribute already
defined in the annotation, which is not very flexible. I don't know if it
would be possible to add an attribute keyValues to the annotation, which
would be an String array. Those values  could be used when evaluating the
key, giving more flexibility to the programmer.

What do you think?
JL

2013/9/25 Lukasz Lenart <lukaszlen...@apache.org>

> 2013/9/25 JOSE L MARTINEZ-AVIAL <jlm...@gmail.com>:
> > Hello,
> >    I have an Action that receives a parameter *email *on a bean *event*.
> So
> > basically the parameter is send to the server with name "*
> > event.email=jlmagc<at>gmail.com*"'. The bean and the parameter are
> created
> > correctly, so this part works ok. Now I want to validate the input using
> > annotations. In order to do that I'm using the following annotation at
> the
> > Action level:
> >
> > @Validations(
> >         requiredStrings =
> >             {@RequiredStringValidator(type = ValidatorType.SIMPLE,
> >                     fieldName = "event.email", key =
> > "validation-error.mandatory")}
> >         )
> >
> >
> > and in the package.properties I have the following definition
> > validation-error.mandatory        = The field ${getText(fieldName)} is
> > mandatory
> > event.email = e-mail
> >
> > With this setup, if the parameter event.email is empty or not present,
> the
> > validator evaluates the expression and returns to the user the following
> > message:
> > "The field e-mail is mandatory".
> >
> > It evaluates the key as an OGNL expression, retrieves the fieldName,
> > evaluates the getTest, and elaborate the sentence correctly. So it works
> ok
> >
> > The issue here is that this forces me to define a property "event.email"
> > since the attribute fieldName in the annotation is used both to locate
> the
> > field that needs validation, and as argument for the getText. Is it
> > possible to split that those two roles, so I can user my already existing
> > property "email", instead of having to duplicate it as "event.email"?
>
> I am not sure what you want to achieve? Maybe some example?
>
> You can always use a different key i.e. validation.error.email.required
> and then
> validation.error.email.required = The field e-mail is required!
>
> What you presented is the situation when you want to use exactly the
> same validator for many fields without specifying key or with the same
> key - it simplifies but isn't flexible ;-)
>
>
> Regards
> --
> Ɓukasz
> + 48 606 323 122 http://www.lenart.org.pl/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
> For additional commands, e-mail: user-h...@struts.apache.org
>
>

Reply via email to