wood burning stoves 2.0*
The moose likes Struts and the fly likes Validation in Struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Validation in Struts" Watch "Validation in Struts" New topic
Author

Validation in Struts

Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392
I am looking for validation on client side for struts. i have few fields which are mandatory and few only text or numbers etc(used html tag libraries)., however my users are impatient to fill all form fields and then recieve errors after submitting(the way struts validation framework works). I am using on onblur or onchange javascript functions can any one think of better solution using JSTL or Struts ( client side validation with out hitting the server again)
Dom Lassy
Ranch Hand

Joined: May 05, 2006
Posts: 181
Struts has built in client side validation.

You should read about struts validation and the tag html:javascript.
Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392
Thanks for the suggestion , my customers keep getting huge number of Fax which they input into html forms and they want instant error message like if a text field needs only number as input and they enter char by mistake they get instant message now ( achieved with java script onChange),
with <html:javascript....> tag i get to show up alerts on clicking of submit is there way i can have more interactive validation with JSTL or Struts tags.. Hope i am clear enough with my requirement
Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392
looks like i will never get any help on this forum ..ummm
Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392
Ud Naik
Greenhorn

Joined: Jun 13, 2006
Posts: 18
looks like i will never get any help on this forum ..ummm


Dom Lassy has already replied to your question. If you want pertinent help, please state problems/requirements clearly.

You can give more 'interactive' messages through javascript. Dont use the struts validations, write your own javascript error handlers for those fields and use the onKeyPress() event (or something similar; I cant remember exactly) for your validation.
Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392
Thanks!

Reply from Lassy really helped i got the struts client side validation working but what trouble i am facing is validating fields as soon as they are filled in ( which can be achieved through script funtions onBlur etc.,) but we have these validations frequently changing so i dont want to write lot of script and change that every time ..,



Can any one share their expertise ... on this if they have achieved this ...
Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392



waiting for reply.. is this possible in struts .., or should go for JSF..
Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392


I am really banging my head with struts... i am still waiting for some reply
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
One possible solution would be to define the validations in the xml file and include the <html:javaScript> tag. Then use your browser's "display source" function to see the JavaScript functions that Struts added to your JSP. You can then call those functions yourself in "onblur" events.


Merrill
Consultant, Sima Solutions
Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392
Good idea..

however i thought the client side javascript gets called on click of submit so not sure if we have options in struts ... to enable validation .. immediately on filling text field etc.,
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by Raghunandan Mamidala:
i thought the client side javascript gets called on click of submit

It does. However, the JavaScript functions are placed in your JSP and you can break out of the normal cycle and call them yourself in an onblur event if you want to. They will still get called at submit time, but they will always pass validation because you've already checked validations in an onblur event.
Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392
Thanks for the help! I got it working

Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392
I have declared following block of xml for requiredif validation

<field property="logNumber" depends="requiredif">
<arg0 key="permits.prototype.walkin.lognumber"/>
<var>
<var-name>field[0]</var-name>
<var-value>appSource</var-value>
</var>
<var>
<var-name>fieldTest[0]</var-name>
<var-value>EQUAL</var-value>
</var>
<var>
<var-name>fieldValue[0]</var-name>
<var-value>Fax</var-value>
</var>
</field>

validation i intend here is lognumber is required, if appSource is Fax. other client side java scrit is working fine. I havent written any validation in validationrules.xml for requiredif and was expecting that this would be validated at server side but this didnt happen ?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I think requiredIf is deprecated in later versions of Struts. I'd suggest you use validWhen instead. This link should explain how to use it. One thing you need to be aware of, though, is that neither requiredIf nor validWhen support client-side validation.
Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392
We are using struts1.1 version plans of migration are bit far off for now, any Idea if there are issues in 1.1 version with requiredif
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Validation in Struts
 
Similar Threads
client side validation vs server side validation
requiredif Validation - Struts Client side validation
struts validator
Validwhen validator client side javascript
Validwhen validator java script