You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Christoph Nenning <Ch...@lex-com.net> on 2015/05/05 13:04:32 UTC

Re: Struts 2 manually call a custom validation in an action

> Please see: http://stackoverflow.com/questions/29603916/struts-2-
> manually-call-a-custom-validation-in-an-action ~Regards,
> ~~Alireza Fattahi
>       From: Alireza Fattahi <af...@yahoo.com.INVALID>
>  To: Struts Mailing List <us...@struts.apache.org> 
>  Sent: Monday, 13 April 2015, 16:40
>  Subject: Struts 2 manually call a custom validation in an action
> 
> We have used struts 2 validation with lots of custom validation to 
> validate our forms. 
> 
>     @Validations( 
>         customValidators =      
>                { @CustomValidator(type = "AccountFormat", fieldName 
> = "accountNo") }
>     )
> 
> Also we can can manually validate a form by overriding the validate 
method
> 
>      public void validate(){
>             //Username can't be blank
>             if(username.equals("")){
>                 addFieldError("username", "The Username can't be 
empty");
>             }
> 
> Is it possible to call the `custom validations` in the `validate()`.
> 
> **Why we need it ?!**
> All validation rules are packed in custom validations, which is 
> perfect. There are some few forms which need to have their own 
> manual validation. We end up cut and pasting some of custom 
> validation rules in these manual validation forms too, it would be 
> best if we could call validations here
>  ~Regards,
> ~~Alireza Fattahi
> 
> 

You could use ExpressionValidator to call action specific validation 
methods.

It would look like:


@Validations( 
   customValidators ={@CustomValidator(type = "AccountFormat")},
   expressions = {@ExpressionValidator(expression="myValidationMethod")}
)



Regards,
Christoph

This Email was scanned by Sophos Anti Virus