You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Steven Hansen <ru...@berkeley.edu> on 2009/06/12 01:15:26 UTC

Manually Checking Validation Errors on an Action

Hi,

I'm trying to test some basic validations on an action:


public class CreateUserAction extends ActionSupport implements 
ModelDriven<User> {
    
    private User user = new User();
    private IUserDao userService = new UserDao();
    
    public String execute() {
        if ( this.userService.save(this.user) ) {
            return SUCCESS;
        }
        else {
            return INPUT;
        }
    }
    
    @VisitorFieldValidator(message="", shortCircuit = false, 
appendPrefix = false)
    public User getModel() {
        return this.user;
    }
    
    public void setModel(User user) {
        this.user = user;
    }
}


Is there anyway to manually check possible validation errors on this 
action as if it were called by the Validation interceptor?

Thanks,
Steven





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


Re: Manually Checking Validation Errors on an Action

Posted by Bhaarat Sharma <bh...@gmail.com>.
you just have to write the validate method

public void validate ()
{

  if (getModel().something()==null)
    addFieldError("question", "Something went wrong");


}

On Thu, Jun 11, 2009 at 7:15 PM, Steven Hansen <ru...@berkeley.edu> wrote:

>
> Hi,
>
> I'm trying to test some basic validations on an action:
>
>
> public class CreateUserAction extends ActionSupport implements
> ModelDriven<User> {
>     private User user = new User();
>   private IUserDao userService = new UserDao();
>     public String execute() {
>       if ( this.userService.save(this.user) ) {
>           return SUCCESS;
>       }
>       else {
>           return INPUT;
>       }
>   }
>     @VisitorFieldValidator(message="", shortCircuit = false, appendPrefix =
> false)
>   public User getModel() {
>       return this.user;
>   }
>     public void setModel(User user) {
>       this.user = user;
>   }
> }
>
>
> Is there anyway to manually check possible validation errors on this action
> as if it were called by the Validation interceptor?
>
> Thanks,
> Steven
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: Manually Checking Validation Errors on an Action

Posted by Steven Hansen <ru...@berkeley.edu>.
> Er... The "validation" interceptor?

Yeah, you're right, duh!  I didn't realize that the validation 
interceptor is actually the AnnotationValidationInterceptor class :-P



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


Re: Manually Checking Validation Errors on an Action

Posted by Dave Newton <ne...@yahoo.com>.
Steven Hansen wrote:
> Are you sure that calling super.validate() from my action's 
> implementation of validate() will invoke the XML/annotation validations?

Hmm, I guess it just does the XML/annotation validation automatically 
without the super. Too late to change it in the book. Oh well... Kinda 
funny, though.

> One of the interceptors in the defaultStack must be analyzing my 
> action's annotations and then setting the appropriate error messages, I 
> just can't figure out which one it is :-(

Er... The "validation" interceptor?

Dave

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


Re: Manually Checking Validation Errors on an Action

Posted by Steven Hansen <ru...@berkeley.edu>.
Are you sure that calling super.validate() from my action's 
implementation of validate() will invoke the XML/annotation validations?

I'm extending ActionSupport, so super.validate() would call 
ActionsSupport's validate(), which is just an empty method ......

But I think you understand what I'm trying to do: I need the errors from 
the ValidationAware interface to get set based on my actions 
@VisitorFieldValidator annotation.

One of the interceptors in the defaultStack must be analyzing my 
action's annotations and then setting the appropriate error messages, I 
just can't figure out which one it is :-(

Thanks!
Steven


Dave Newton wrote:
> Steven Hansen wrote:
>> Is there anyway to manually check possible validation errors on this 
>> action as if it were called by the Validation interceptor?
>
> The previous answer was partially correct, depending on what you're 
> actually trying to do. If you implement a validate() method you can 
> call super.validate() which will run XML/annotation validations. From 
> there you can call hasFieldErrors()/hasActionErrors()/etc. to 
> determine if there were any.
>
> Dave
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>


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


Re: Manually Checking Validation Errors on an Action

Posted by Dave Newton <ne...@yahoo.com>.
Steven Hansen wrote:
> Is there anyway to manually check possible validation errors on this 
> action as if it were called by the Validation interceptor?

The previous answer was partially correct, depending on what you're 
actually trying to do. If you implement a validate() method you can call 
super.validate() which will run XML/annotation validations. From there 
you can call hasFieldErrors()/hasActionErrors()/etc. to determine if 
there were any.

Dave

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