aspose file tools*
The moose likes Struts and the fly likes validation.xml  - how to ignore the required fields if a checkBox was clicked? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "validation.xml  - how to ignore the required fields if a checkBox was clicked?" Watch "validation.xml  - how to ignore the required fields if a checkBox was clicked?" New topic
Author

validation.xml - how to ignore the required fields if a checkBox was clicked?

Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
Hi guys,

My form is composed of 2 options: either to pay with a credit card or with a check.
If you pay with a check simply click the Check Box and you are done. If you didn�t � that means you are paying with a credit card.

1. How to instruct the validation.xml to do this: if the checkbox is selected � don�t bother checking for errors on the credit card (required fields).

2. The credit card is composed of typeOfCreditCard and it�s a drop down box (code below). Since this is a required field � how can I instruct the validation.xml that 'Select One' is not a valid input?

Thank you
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
1. Use the validWhen validation rule. This link explains how to use it.

This will work only if you're just validating that some value was placed in the credit card number field. If you need to do a check-sum or anything more sophisticated than that, you will have to write your own custom validation rule in order to make this work.

2. Code the "Select One" option like this:

<htmlption value="">Select One</htmlption>

If you do it this way, the required validation will work because the value of creditType will be a null string if this option is selected.


Merrill
Consultant, Sima Solutions
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
thanks - number 2 was easy :-)

as per the validwhen, I did this:

validator.xml




struts-config:


JSP:


but, it doesn't work...it produce the error message either way.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Your logic is faulty. Your test should be:

(check == true) or (*this* != null)

Think about it: If the checkbox is checked (true), the cardType field is valid whether it has a value or not. If the checkbox is not checked (false) the cardType is valid only if it has a value.
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
I tried all different combinations....none works.
I realized that when I type �validwhen� in the validation.xml the error.creditType.required doesn�t work. Meaning even if I click (or not) the checkbox (in the jsp page) nothing happens � no error message.

How can I test if the validwhen works properly?

vlidation.xml


struts-config


jsp
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Two questions:

1-Do you have entries in your ApplicationResource.properties file for both errors.required and error.creditType.required ? An errors.required message key is needed by the validWhen validation rule.

2-Do you have an <html:errors /> tag in your JSP?
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
1. Yes:

errors.required={0} is required.
error.creditType.required=Type

2. Yes:

Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Try changing the expression to:

((check == 'true') or (*this* != null))

I was just looking at the list of expressions allowed in the test, and a boolean true or false is not on the list. If this doesn't work, try check != null.
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
...I tried lots of combinations....nothing.

the only thing that works is when I replace the validwhen with required (I also tried validwhen,required...nothing) but when i do that, there's no 'if statement'.

is there anything else I need to do before using the validwhen?

also...I got more pessimistic after I read this.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I don't know what to tell you except that this should work. Since it does't seem to, I'd suggest you write a custom validation rule to cover the situation.
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
I'll give it another try.

I wonder, wouldn't it be easier if I'll write a regular ActionForm (not dyna) and validate it with simple java command?

Thank you for all your help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: validation.xml - how to ignore the required fields if a checkBox was clicked?
 
Similar Threads
How can I add more text input base on users selection
how to validate
drop down list validation
Struts Validation Problem
locale attribute in html:errors is not working