validation with annotation

View: New views
5 Messages — Rating Filter:   Alert me  

validation with annotation

by xianwinwin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi there,

I would like to create a validation with annotation.
consider the following case:

@Validation
public class AddCaseAction
{
        private Registration registration;
.
.
.

        public String addNewRegistration()
        {
                //do something
                return "success";
        }

// get and setRegistration here
}

assuming registration is composed of fname, lname, ss, dob and except dob all fields are mandatory.

Question: how do I make sure those fields will be input? should I have something like:

@RequiredStringValidator(message="Legal Name is missing")
private setFname(String fn)

on the registration - if so - wouldn't that effect other classes that uses registration???

thanks for any input!

Re: validation with annotation

by Ealden Escañan :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, Mar 11, 2008 at 7:10 AM, xianwinwin <xianwinwin@...> wrote:

>
> assuming registration is composed of fname, lname, ss, dob and except dob
> all fields are mandatory.
>
> Question: how do I make sure those fields will be input? should I have
> something like:
>
> @RequiredStringValidator(message="Legal Name is missing")
> private setFname(String fn)
>
> on the registration - if so - wouldn't that effect other classes that uses
> registration???
>
> thanks for any input!

Hi xianwinwin,

You can annotate an action method with @Validations and validate from there, ie:

@Validations(
    requiredString = {
        @RequiredFieldValidator(
            type=ValidatorType.FIELD,
            fieldName="registration.fname"
            message="Foo!"
        )
    }
)
public String save() { ... }

http://struts.apache.org/2.x/docs/validation-annotation.html

Admittedly, I used this approach these past few days and found myself
back to validations in XML:
http://blog.ealden.net/2008/03/working-with-struts-2-validators.html

Thanks

--
Ealden Esto E. Escañan
http://blog.ealden.net

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


Re: validation with annotation

by xianwinwin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Ealden, this was very helpful!

I tried that and it works fine.

Question, say I wish to check for a date, so far I've been doing it this way:
example

private Date delta;

        @TypeConversion(converter="com.utilities.conversion.DateTypeConversion")
        public void setDelta(Date delta)
        {
                this.delta = delta;
        }

Obviously the I created the converter. How can I incorporate this TypeConversion into the Action Annotation (say to field registration.dob)?

 @Validations(...what comes here.???...)
public String save()
{
   //some code
}


Thank you!

Re: validation with annotation

by Ealden Escañan :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Mar 12, 2008 at 4:17 AM, xianwinwin <xianwinwin@...> wrote:

>
>  Thanks Ealden, this was very helpful!
>
>  I tried that and it works fine.
>
>  Question, say I wish to check for a date, so far I've been doing it this
>  way:
>  example
>
>  private Date delta;
>
>         @TypeConversion(converter="com.utilities.conversion.DateTypeConversion")
>         public void setDelta(Date delta)
>         {
>                 this.delta = delta;
>         }
>
>  Obviously the I created the converter. How can I incorporate this
>  TypeConversion into the Action Annotation (say to field registration.dob)?
>
>   @Validations(...what comes here.???...)
>  public String save()
>  {
>    //some code
>  }
>

Xianwinwin,

There should be a conversion error interceptor in, say, defaultStack,
that prompts the user if a conversion fails such as when the date
entered is not correct.  A case that needs to be handled though is if
the user didn't enter any date at all which doesn't seem to cause a
conversion error.  Since you are using a custom type converter I
suppose you can catch this case.

Thanks

--
Ealden Esto E. Escañan
http://blog.ealden.net

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


Re: validation with annotation

by matt.payne :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The current usage of Annotating methods seems broken.  
Its still applying all the validation rules to all methods despite methods having different validation requirements.

The Struts "Zero Config/Annotation/Auto Config needs help"

Hopefully, a sprinkling of sane configuration of inheritance (though xml) will add power, and reduce redundant configuration code.

re: http://jira.opensymphony.com/browse/XW-603

Matt

Ealden Escañan wrote:
On Tue, Mar 11, 2008 at 7:10 AM, xianwinwin <xianwinwin@gmail.com> wrote:
>
> assuming registration is composed of fname, lname, ss, dob and except dob
> all fields are mandatory.
>
> Question: how do I make sure those fields will be input? should I have
> something like:
>
> @RequiredStringValidator(message="Legal Name is missing")
> private setFname(String fn)
>
> on the registration - if so - wouldn't that effect other classes that uses
> registration???
>
> thanks for any input!

Hi xianwinwin,

You can annotate an action method with @Validations and validate from there, ie:

@Validations(
    requiredString = {
        @RequiredFieldValidator(
            type=ValidatorType.FIELD,
            fieldName="registration.fname"
            message="Foo!"
        )
    }
)
public String save() { ... }

http://struts.apache.org/2.x/docs/validation-annotation.html

Admittedly, I used this approach these past few days and found myself
back to validations in XML:
http://blog.ealden.net/2008/03/working-with-struts-2-validators.html

Thanks

--
Ealden Esto E. Escañan
http://blog.ealden.net

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