You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Ruimo Uno <ru...@gmail.com> on 2007/06/13 10:46:11 UTC

How to validate field in type of a model class by annotations.

Hi, my action has a field that is in type of User:

    User user;
    public User getUser() {return user;}
    public void setUser(User user) {this.user = user;}

And the following is a validation rule for it using XML file (Assume
the User class has 'email' field.):

  <field name="user.email">
    <field-validator type="requiredstring" short-circuit="true">
      <message key="email.required"/>
    </field-validator>
    <field-validator type="email">
      <message key="email.invalid"/>
    </field-validator>
  </field>

It worked fine. And now, I am trying to use annotations. However, I
have no idea to specify @EmailValidator to the 'user' field in the
action. The following did not work:

    User user;
    public User getUser() {return user;}
    @EmailValidator(type = ValidatorType.FIELD,
                    fieldName = "user.email", key = "email.invalid",
                    message = "Invalid mail addrss")
    public void setUser(User user) {this.user = user;}

I noticed that using @Validations, I can specify the @EmailValidation
as a validation rule for whole class but the speicif field. But I
cannot specify short-circuit in @Validations.

Any idea?

-- 
Ruimo Uno
(Shisei Hanai)

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: How to validate field in type of a model class by annotations.

Posted by Ruimo Uno <ru...@gmail.com>.
Thank you for your information. I have got xwork-2.0.3 and took diff
against 2.0.1 in directory
'src/java/com/opensymphony/xwork2/validator/annotations'. It seems
that the only change is just removing StringRegexValidator. Sounds
like I should wait until 2.1 release.

2007/6/13, Jeromy Evans <je...@blueskyminds.com.au>:
>    public User getUser() {return user;}
> >    @EmailValidator(type = ValidatorType.FIELD,
> >                    fieldName = "user.email", key = "email.invalid",
> >                    message = "Invalid mail addrss")
> >    public void setUser(User user) {this.user = user;}
> >
> > I noticed that using @Validations, I can specify the @EmailValidation
> > as a validation rule for whole class but the speicif field. But I
> > cannot specify short-circuit in @Validations.
> >
> > Any idea?
> >
> In 2.0.6 the annotation validation has a lot of limitations compared to
> the xml version.  For a start it doesn't work with multiple action
> methods properly, I'm fairly sure it can't be used with the DWR ajax
> validator and I'm fairly sure you can't use 'fieldName = "user.email" on
> a User property.  I'm not 100% sure because I stopped bothering with it
> as XML works just fine.
>
> However, there's been many improvements to annotation validation in
> xwork 2.0.2 and 2.1 in the last few months (see the issues at [1], maybe
> yours is mentioned), and great improvements to ajax validation for
> struts 2.1, so I don't know the status any more.  Struts 2.0.6 uses
> xwork 2.0.1 but Struts 2.0.8 uses xwork 2.0.3.
>
> In summary, it probably should work but doesn't, so maybe try Struts 2.0.8.
>
> [1] http://jira.opensymphony.com/secure/Dashboard.jspa   (search for
> annotation validation)
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


-- 
Ruimo Uno
(Shisei Hanai)

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: How to validate field in type of a model class by annotations.

Posted by Jeromy Evans <je...@blueskyminds.com.au>.
   public User getUser() {return user;}
>    @EmailValidator(type = ValidatorType.FIELD,
>                    fieldName = "user.email", key = "email.invalid",
>                    message = "Invalid mail addrss")
>    public void setUser(User user) {this.user = user;}
>
> I noticed that using @Validations, I can specify the @EmailValidation
> as a validation rule for whole class but the speicif field. But I
> cannot specify short-circuit in @Validations.
>
> Any idea?
>
In 2.0.6 the annotation validation has a lot of limitations compared to 
the xml version.  For a start it doesn't work with multiple action 
methods properly, I'm fairly sure it can't be used with the DWR ajax 
validator and I'm fairly sure you can't use 'fieldName = "user.email" on 
a User property.  I'm not 100% sure because I stopped bothering with it 
as XML works just fine.

However, there's been many improvements to annotation validation in 
xwork 2.0.2 and 2.1 in the last few months (see the issues at [1], maybe 
yours is mentioned), and great improvements to ajax validation for 
struts 2.1, so I don't know the status any more.  Struts 2.0.6 uses 
xwork 2.0.1 but Struts 2.0.8 uses xwork 2.0.3. 

In summary, it probably should work but doesn't, so maybe try Struts 2.0.8.

[1] http://jira.opensymphony.com/secure/Dashboard.jspa   (search for 
annotation validation)



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org