Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Yes:

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

2. Yes:

 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic