Looks like you want some pre-condition check before the validation.
If and only if the pre-condition is true, the validation will be executed.

I did the same thing several years ago.

There are 2 solution I tried:
(A)Inherit all the struts2 bundled validators, add some pre-condition
check code,
if and only if the pre-condition is true, invoke the method of the
base class. The base class (struts2 bundled validators) will do the
left work.

BTW, the pre-condition should be passed into the validators via
<param>, so the validators will not include any business logic and
they can be reused.
And since your pre-condition is a session attribute, I think you can
check it's value by OGNL expression. I am not very sure for this. If
you can not access session attribute via OGNL, maybe you need a
[getCountry] method in your action class, so the OGNL can retrieve the
value.

You don't need reinvent the whole wheels, but extend the existed wheels.

Source code like:
public class XXXValidatorEx extends XXXValidator {
     private String condition;  // here is the pre-condition in OGNL

     public getCondition(); // omitted
     public setCondition(); // omitted

     private boolean evaluateCondition(){
         // omitted
         // you can read the code of [ExpressionValidator] and
[FieldValidatorSupport], to learn how to evaluate an OGNL expression.
     }

     public void validate(Object object) {
           boolean result = this.evaluateCondition();
           if (result ) {
               super.validate(object);
           }
     }
}

There are about one dozen bundled validators, and the source code of
the inherited validators will be almost 100% same (only except the
class name and the base class name).
This will be a little ugly, but we have no choice, because Java don't
support multi-inherit or interface-delegation.
My choice was, generating all these source codes via FreeMarker, so I
need maintenance only one template file, but not one dozen of class
source codes.


(B)The other solution is much lighter.
No need the enhance the validator class, but express your
pre-condition and your validation rule all by expression.

like:
<validator type="expression">
     <param name="expression">(!pre-condition) or (check-rule)</param>
</validator>

This will be easy to do, but hard to use,
because you must translate all the validation rules into some OGNL expression



Anyway, I think the final and most beautiful solution should be,
adding some [pre-condition checking] into the Struts2 bundled
validators themselves.
But this is out of our control, hope the developers of Struts2 can here this.



2011/11/28 Jyothrilinga Rao <jyoth...@gmail.com>:
> Thanks Li Ying,
> I got some information from
> http://today.java.net/pub/a/today/2006/01/19/webwork-validation.html?page=3#custom-validators
> but I do not want to reinvent the wheel (basically avoid coding my
> validation logic in java code). If I
> could have my <actionClass>-validation.xml structured as
>
> <validators>
> *<country name="USA">
> *  <!-- Field Validators for email field -->
>  <field name="email">
>      <field-validator type="required" short-circuit="true">
>          <message>You must enter a value for email.</message>
>      </field-validator>
>      <field-validator type="email" short-circuit="true">
>          <message>Not a valid e-mail.</message>
>      </field-validator>
>  </field>
>  <!-- Plain Validator 1 -->
>  <validator type="expression">
>      <param name="expression">email.equals(email2)</param>
>      <message>Email not the same as email2</message>
>  </validator>
>  *</country>*
> *<country name="AUSTRIA">
> *   <!-- Field Validators for email2 field -->
>  <field name="email2">
>     <field-validator type="email">
>          <message>Not a valid e-mail2.</message>
>      </field-validator>
>  </field>
>  <!-- Plain Validator 2 -->
>  <validator type="expression" short-circuit="true">
>      <param name="expression">email.startsWith('mark')</param>
>      <message>Email does not start with mark</message>
>  </validator>
> *</country>*
> </validators>
>
> I am looking for something like a wrapper on existing bundled validators.
> Is it possible.
>
> Regards,
> JK.

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

Reply via email to