You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by RogerV <ro...@googlemail.com> on 2010/12/16 11:29:31 UTC

Struts 2 Conditional Validators?

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-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts 2 Conditional Validators?

Posted by RogerV <ro...@googlemail.com>.


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

Ok - bad choice of words. Validation annotations are processed by the
AnnotationValidationInterceptor. How it decides what to process I find a bit
hazy. There is super class ValidationInterceptor call made
actionValidatorManager.validate(action, context, method); 
which I assumed meant that the interceptor was looking to process only those
annotations on the method being called, rather than looking for all the
annotations in the class and applying them in turn (which is what the
VistorValidator seems to do unfortunately). I kind lost it at this point as
Struts found and executed my annotations although they were on methods
called by execute() rather than being placed on execute() itself.

Regards

Regards
-- 
View this message in context: http://old.nabble.com/Struts-2-Conditional-Validators--tp30471468p30472121.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: Struts 2 Conditional Validators?

Posted by Dave Newton <da...@gmail.com>.
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 <ro...@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-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