aspose file tools*
The moose likes Struts and the fly likes Struts wizard + JavaScript validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts wizard + JavaScript validation" Watch "Struts wizard + JavaScript validation" New topic
Author

Struts wizard + JavaScript validation

Velika Srbija
Ranch Hand

Joined: Jan 15, 2002
Posts: 172
In the second step of my wizard I have made a JSP page containing several <html:form ...> elements.
Every element point's to different Action class but use's the same instance (under the same name) of the ValidatorForm bean.
I have set hidden page property on every <html:form ...> element to value="2", configured validation.xml as appropriate etc.
Server-side validation is working work perfectly, but JavaScript validation doesn't occur at all.
In fact, JavaScript routines are generated as expected, but validation doesn't occur when some of the forms is submited.

Note:
Despite tutorials, books etc., I had to set page property in <html:javascript... tag in order to have JavaScript validation even for the first step of my wizard (<html:javascript formName="appHSLAForm" page="1"/> .


Here some excerpts from my code:
********************************

step2.jsp
---------
...
<html:javascript formName="appForm" page="2" />
...
<html:form action="action1.do" onsubmit="return validateAppForm(this)" >
<html:hidden property="page" value="2"/>
<html:text property="text_1_1" size="6" value=""/>
<html:text property="text_1_2" size="6" value=""/>
<html:submit>
<bean:message key="app.ok"/>
</html:submit>
</html:form>
...
<html:form action="action4.do" onsubmit="return validateAppForm(this)" >
<html:hidden property="page" value="2"/>
<html:text property="text_4_1" size="6" value=""/>
<html:text property="text_4_2" size="6" value=""/>
<html:submit>
<bean:message key="app.ok"/>
</html:submit>
</html:form>
...
...

struts-config.xml
-----------------
...
<form-bean name="appForm" type="app.Form"/>
...
<action path="/action1"
type="app.Action1"
name="appForm"
scope="session"
input="/web/app/step2.jsp"
validate="true">
<forward name="success" path="/web/app/success.jsp"/>
</action>
...
<action path="/action4"
type="app.Action4"
name="appForm"
scope="session"
input="/web/app/step2.jsp"
validate="true">
<forward name="success" path="/web/app/success.jsp"/>
</action>
...
...

validation-rules.xml
--------------------
...
<form name="appForm">
<field property="text_1_1" depends="required" page='2'>
<arg0 key="app.validator.error.text_1_1"/>
</field>
<field property="text_1_2" depends="required" page='2'>
<arg0 key="app.validator.error.text_1_2"/>
</field>
... ...
<field property="text_4_1" depends="required" page='2'>
<arg0 key="app.validator.error.text_4_1"/>
</field>
<field property="text_4_1" depends="required" page='2'>
<arg0 key="app.validator.error.text_4_1"/>
</field>
...

ValidatorForm bean with getters and setters for all properties has been written well. No need to present it here.

##### Is there something special I should pay my attention in order to have JavaScript validaton work OK?
Sheldon Fernandes
Ranch Hand

Joined: Aug 18, 2004
Posts: 157
I have a question for you regarding how server-side validation is currently being done. Suppose you submit the first form "action1.do", then only text_1_1 and text_1_2 get submitted. Then wouldn't you get the "Required" validation server-side errors during server-side validation for fields text_4_1 and text_4_2. I say this because they all have the same page number. I hope my understanding is correct. If it is not, ignore what I have said below.

I would suggest using ValidatorActionForm for the below scenario. No need of page numbers on each page's forms. Just change the validation.xml so that the form names are now the action-mapping "path".
For example:


Hope this helps.

If you do figure out what the problem is with your current implementation, do let us know.

Sheldon Fernandes
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
I also want to know how this be used. As we are currently just using *real* JavaScript to check the values, not really depends on the JS tag.

Thanks

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Velika Srbija
Ranch Hand

Joined: Jan 15, 2002
Posts: 172
Sorry. I have copied one of my test/debug implementations which I used to figure out what is goin' on.? Sheldon Fernandes and Nicholas Cheung you were right. I see you understod the problem correctly. Thanks.

This is correct code:
validation-rules.xml
--------------------
...
<form name="appForm">
<field property="text_1_1" depends="float" page='2'>
<arg0 key="app.validator.error.text_1_1"/>
</field>
<field property="text_1_2" depends="float" page='2'>
<arg0 key="app.validator.error.text_1_2"/>
</field>
... ...
<field property="text_4_1" depends="float" page='2'>
<arg0 key="app.validator.error.text_4_1"/>
</field>
<field property="text_4_1" depends="float" page='2'>
<arg0 key="app.validator.error.text_4_1"/>
</field>
...

Note: With examples shipped with the book "Struts in action" there is Struts 1.1 release with validation rules all set to required by default. So float was required by default. That made me many problems until I switched to the latest version of the Struts 1.1. Now everything work's well, except JS.
------------------------------------
I also tried to attach the very same Form bean with different names to my Actions. That doesn't worked. E.g.:
...
<form-bean name="appForm" type="app.Form"/>
<form-bean name="appForm1" type="app.Form"/>
...
<form-bean name="appForm4" type="app.Form"/>
...
<action path="/action1"
type="app.Action1"
name="appForm1"
scope="session"
input="/web/app/step2.jsp"
validate="true">
<forward name="success" path="/web/app/success.jsp"/>
</action>
...
<action path="/action4"
type="app.Action4"
name="appForm4"
scope="session"
input="/web/app/step2.jsp"
validate="true">
<forward name="success" path="/web/app/success.jsp"/>
</action>
...
...
Velika Srbija
Ranch Hand

Joined: Jan 15, 2002
Posts: 172
Pay attention on the fact that I have to set page attribute in the html:javascript tag in order to have validation done at the first step.

What do you think about that?
Velika Srbija
Ranch Hand

Joined: Jan 15, 2002
Posts: 172
I left only one (the first one) html:form on the JSP:
...
<html:javascript formName="appForm" page="2" />
...
<html:form action="action1.do" onsubmit="return validateAppForm(this)" >
<html:hidden property="page" value="2"/>
<html:text property="text_1_1" size="6" value=""/>
<html:text property="text_1_2" size="6" value=""/>
<html:submit>
<bean:message key="app.ok"/>
</html:submit>
</html:form>
...

and changed validation-rules.xml as:
...
<form name="appForm">
<field property="text_1_1" depends="required" page='2'>
<arg0 key="app.validator.error.text_1_1"/>
</field>
<field property="text_1_2" depends="required" page='2'>
<arg0 key="app.validator.error.text_1_2"/>
</field>
... ...
<field property="text_4_1" depends="required" page='3'>
<arg0 key="app.validator.error.text_4_1"/>
</field>
<field property="text_4_1" depends="required" page='3'>
<arg0 key="app.validator.error.text_4_1"/>
</field>
...

and JavaScript on the JSP worked fine.

So, definitely, many html:form tags on a single JSP, all of them pointing to different actions but the same ValidatorForm bean, with all properties on the JSP that belong to the same step of the wizard flow lead to JavaScript validation not functioning. (Server-side validation work's well. )

It seem's it is a bug in the Validator framework.
Sheldon Fernandes
Ranch Hand

Joined: Aug 18, 2004
Posts: 157
Originally posted by Velika Srbija:
Pay attention on the fact that I have to set page attribute in the html:javascript tag in order to have validation done at the first step.

What do you think about that?


Good find, I did not know that. I have not yet implemented a wizard type validation, though I read how its supposed to be done. You're right, most tutorials/documentation do not mention it.
Velika Srbija
Ranch Hand

Joined: Jan 15, 2002
Posts: 172
So, what shall we do now?
Is there any workaround?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Struts wizard + JavaScript validation