Validators aren't called when *you* call a method, its not like they
cause byte-code to be inserted.

Dave

On Thursday, December 16, 2010, RogerV <roger.var...@googlemail.com> wrote:
>
> Hi
>
> Is it possible to "conditionally" trigger a validator such that if
> conditionA then use ValidatorX else use ValidatorY within the validation
> framework out of the box?
>
> I have a scenario where a user is entering a serial number as a license key.
> They can either enter a full serial number which licenses a particular
> machine, or they can enter a "generic" license number, for example "DEF*"
> which licenses all machine of a particular model. These keys end up in the
> same column in the database and there is a single "serial Number field" in
> the data capture form and hence a single attribute in my action. And of
> course, they need validating differently.
>
> The obvious solution to create two seperate input fields on the .jsp form
> has been vetoed by the client. I have tried calling two seperate methods and
> attaching the validators to the individual methods like;
>
> public String execute() {
>                 if (licenseId.endsWith("*")) {
>                         addGenericLicense(licenseId);
>                 } else {
>                         addSpecificLicense(licenseId);
>                 }
>                         return SUCCESS;
>         }
>
>
>
> @CustomValidator(fieldName="licenseId",type="SpecificLicense",message="Invalid
> License key")
>
>         private void addSpecificLicense(String licenseId) {
>                 getNewUserDTO().addSpecificLicense(licenseId);
>
>         }
>
>
> @CustomValidator(fieldName="licenseId",type="GenericLicense",message="Invalid
> Generic License key")
>         private void addGenericLicense(String licenseId) {
>                         getNewUserDTO().addGenericLicense(licenseId);
>
>         }
>
> but that doesn't work either - it looks like when either method
> (addGenericLicense() or addSpecificLicense() is executed, all the validators
> for the field licenseId are called and both validators fire.
>
> I could of course, just write a validate() method in my action, but I would
> like to keep the validators as I can re-use them elsewhere, so if there's no
> way to have an "either/or validator" through the framework, I was wondering
> if there is way of "nesting" the validators. Something along the lines of
> having one single validator for the license key that programmatically calls
> the correct validator internally? After looking at the ValidationInterceptor
> I've had a try at calling a validator programmatically within my action with
>
> if (licenseId.endsWith("*")) {
>                         GenericLicenseValidator validator = new 
> GenericLicenseValidator();
>                         validator.setValidatorContext(new 
> DelegatingValidatorContext(this));
>                         validator.setFieldName("licenseId");
>                         try {
>                                 validator.validate(this);
>                         } catch (ValidationException e) {
>                                 e.printStackTrace();
>                         }
>                 }
>
> but this is giving me null pointer exceptions and I don't know if this is
> because my code is wrong or because it just insn't going to work. So if
> anyone could point me to examples of programmatically calling a validator I
> would appreciate it.
>
> Regards
>
>
>
>
> --
> View this message in context: 
> http://old.nabble.com/Struts-2-Conditional-Validators--tp30471468p30471468.html
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
> For additional commands, e-mail: user-h...@struts.apache.org
>
>

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

Reply via email to