This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Validation in Struts

 
Sri Anand
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Struts has built in client side validation.

You should read about struts validation and the tag html:javascript.
 
Sri Anand
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
looks like i will never get any help on this forum ..ummm
 
Sri Anand
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ud Naik
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



waiting for reply.. is this possible in struts .., or should go for JSF..
 
Sri Anand
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I am really banging my head with struts... i am still waiting for some reply
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sri Anand
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help! I got it working

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