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

Struts 2.1 javascript field validate

E Robb
Ranch Hand

Joined: Aug 27, 2010
Posts: 111
I am unable to get my Struts 2.1 application to do javascript validation on a simple form. When I submit the form no validation is carried out and no errors are thrown in the stack trace. I must be missing something really simple here can someone look and let me know what they think?

In WEB-INF/classes/candidate I have a file called Security-validation.xml


The form is in WebContent/candidate and contains:


My struts.xml contains:


The action is in a folder called common and the action name is Security.java:
Jesus Mireles
Ranch Hand

Joined: Mar 10, 2010
Posts: 122

I'm pretty sure that you should have the Security-validation.xml in WEB-INF/classes/common since that is where your action is located.
E Robb
Ranch Hand

Joined: Aug 27, 2010
Posts: 111
Pure genuis Im so new to struts have been googling all day long for this. Would have taken me forever to figure out. Looked in Struts 2 in action & Apache Struts 2. Thank you Thank you Thank you!

I am now getting javascript pop ups after I moved my Security-validation.xml to the commons folder as you said Lucas. Wooo Ho!!! I wont soon forget that lesson.

However for IE 8 I get javascript pop ups and for Firefox I get web page side validation. I suppose that in Firefox I am not getting client side validation because the FF Error log shows Error: document.security is undefined
Source File: http://localhost:8181/AdaptWSC_v3/candidate/security.jsp
Line: 1

I have attached a screen shot of the js pop up error I get in windows 7 ie 8. It is closer to what I would expect but does not have the text from the properties files. The screen shot also shows what FF returns. One browser is returning a pop up while the other is still returning a field based validation. I have restarted jboss & dumped the cache to be sure everything is out of cache.

This is how I have changed the arrangement to get IE to give me the java script pop up errors:

1. Moved candidate/Security-validation.xml to common/Security-validation.xml

2. Changed the struts.xml to:


And three changed the form to conform with the new struts.xml


[Thumbnail for struts_errors.jpg]

Jesus Mireles
Ranch Hand

Joined: Mar 10, 2010
Posts: 122

You're actually getting the error in both browsers but IE is just more painful ... *I think* that your issue is because of the theme you are using. According to the documentation the validate attribute in the form tag is only useful when you have the xhtml/ajax theme. In your case you are using css_xhtml theme.

http://struts.apache.org/2.2.1/docs/form.html

As you can see the description for the validate is "Whether client side/remote validation should be performed. Only useful with theme xhtml/ajax"

So I think the javascript is not getting generated correctly because of the theme you choose for this form.

E Robb
Ranch Hand

Joined: Aug 27, 2010
Posts: 111
I've thought about this a bit more and I think I am asking the wrong question. My apologies.

I did some more testing and it appears the AJAX and the javascript validation is working correctly with the help of Lucas. Once the validation is completed the javascript manipulates the DOM of the html document and puts the messages beside the html fields. What I would like to happen is to get a javascript alert() window that has a list of fields that are not correct. Is there a way to have struts use alert() rather than manipulate the DOM and put the messages back in the HTML?

Thanks
Earl
Jesus Mireles
Ranch Hand

Joined: Mar 10, 2010
Posts: 122

I personally only use client side validation when I have a requirement to do so. So try the following:

1) Change your form to:



All your errors will show up above the form and the only diff between my form tag and yours is that I removed validate="true"

This should still validate just not on client side.


E Robb
Ranch Hand

Joined: Aug 27, 2010
Posts: 111
True that Lucas Ive done it on another form and it works great. Im setting up a demo to try and get our company to go to a framework instead of script kiddy jsp's. If we cant get java script validation with the framework its going to be a show stopper.

Im exploring the AJAX you mentioned further. Our parent company is running .NET with c# using a MVC and this sort of thing doesnt seem to be an issue. BUT Im trying to port a spagettie site to a MVC framework and I have NO idea what Im doing other than two books & this site.

Thank you for your help.
E Robb
Ranch Hand

Joined: Aug 27, 2010
Posts: 111
I did some more testing and it appears the AJAX and the javascript validation is working correctly with the help of Lucas. Once the validation is completed the javascript manipulates the DOM of the html document and puts the messages beside the html fields. What I would like to happen is to get a javascript alert() window that has a list of fields that are not correct. Is there a way to have struts use alert() rather than manipulate the DOM and put the messages back in the HTML?

Thanks
Earl
Jesus Mireles
Ranch Hand

Joined: Mar 10, 2010
Posts: 122

I'm not sure that there is any type of out of the box support for this. You may need to download the source code and modify what you need to so that it renders as javascript alerts.

You may also be able to use a javascript framework such as JQuery or YUI to listen for events that fire when your DOM changes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts 2.1 javascript field validate