File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes checkbox validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "checkbox validation" Watch "checkbox validation" New topic
Author

checkbox validation

Hamish Rose
Greenhorn

Joined: Dec 14, 2005
Posts: 6
Hi Folks

following on from Ricardo's post - 'how to validate boolean field...' but that thread seems to have gone cold (my addition hasn't flagged up in the latest posts list) so I'll try again here - appologies if this is missuse

I'm trying to validate the same checkbox scenario but am obviously still doing something wrong. I had made the same mistake using html input tags instead of struts html:checkbox tags. However rectifying this hasn't made any difference at all. Below are the relevant bits out of my code, if someone can tell me where I'm going wrong it'd be much appreciated. Thanks in advance, Hamish.

(NB further validation of text fields in the same multi page form do work)

from jsp...

<html:hidden property="page" value="1"/>
<td colspan="5" class="bodyHeader"><bean:message key="checkout.custDetails.lable.acceptTerms"/><div class="bodyError"><html:errors property="acceptTerms"/></div></td>
<td></td>
<td colspan="3" align="right"><img src="<%=request.getContextPath()%>/images/rightArrow.gif" border="0"/> <bean:message key="cart.link.terms"/></td>
<td></td>
<td><html:checkbox property="acceptTerms"/></td>

from validate.xml...

<form name="orderForm">
<field
property="acceptTerms"
page="1"
depends="required">
<arg0 key="checkout.error.acceptTerms"/>
</field>

reset method in action form (which extends ValidatorForm)...

public void reset(){
this.setAcceptTerms(false);
}
bhuvan sundar
Ranch Hand

Joined: Mar 02, 2005
Posts: 104
try adding checkout.error.acceptTerms=msg in resources file
Hamish Rose
Greenhorn

Joined: Dec 14, 2005
Posts: 6
cheers but unfortunately I already had and even if I hadn't the server should have returned to the page with the checkbox on (letting me know it was doing something) rather than gaily continuing as if nothing was wrong - it seems to be accepting an unchecked box as valid the same as a checked one.

Ultimately I could avoid the problem by either using I Accept / I don't accept radio buttons or with a string value multibox but as others have managed to get this to work I'd like to understand how / why, rather than avoid the problem

H
bhuvan sundar
Ranch Hand

Joined: Mar 02, 2005
Posts: 104
<html:errors property="acceptTerms"> next to <bean:message>-does that mean that there is another input field by the name "acceptTerms". If that is the case, then, try changing the property value of <html:checkbox>
Hamish Rose
Greenhorn

Joined: Dec 14, 2005
Posts: 6
No there is one property in the ActionForm called acceptTerms - the <html:checkbox> should both populate it and be populated by it and the <html:errors> tag should simply display a message from the resourcesbundle if validation on the input (checkbox) for this property fails - they have to have the same name or the error message displayed would relate to a different input field.

ta H
Hamish Rose
Greenhorn

Joined: Dec 14, 2005
Posts: 6
Hi again folks and folkettes

in true forum style I've fixed it myself but for those who're interested here's how I did it... I still have no idea how Ricardo did it, if you're there Ricardo let us know??

I added a validwhen clause in validate.xml, as follows

<field
property="acceptTerms"
depends="required, validwhen"
page="1" >
<arg0 key="checkout.error.acceptTerms"/>
<var>
<var-name>test</var-name>
<var-value>(*this* == "true")</var-value>
</var>
</field>

as far as I can tell it validates the depends="required" regardless cos the box either has a value of true or false but always has one (??? not how I thought they were handled) and therefore you have to test for true, but booleans aren't allowed, however fortunately you can test for the string "true" and it seems to work - hey!

cheers for the help, hope this helps some other frustrated sole

H
 
Consider Paul's rocket mass heater.
 
subject: checkbox validation
 
Similar Threads
Struts validation not working
How to display error message
How to Validate duplicate entries in database using struts1
How to validate a boolean field using struts validation
Validation framework not working for login screen