Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Struts form - validation

Posted on 2004-08-16
11
Medium Priority
?
1,468 Views
Last Modified: 2012-08-13
Hai,

Is onchange="this.form.submit();" in SELECT tag allowable for Struts form? But it always shows JS errors for me.

If it is allowed(hopefully):
-----------------------------

I have a form with 2 text fields an two dynamic SELECTION boxes. Struts FormBean validates the textfields such that it should not empty. If it is empty, an error message saying that 'Field1 is Empty".

What i want is: If onchange="this.form.submit();" event occurs for one selection box, this error message should not display.

Is it possible or any other solutions?

Thanks.
0
Comment
Question by:sangeetha
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 14

Expert Comment

by:boonleng
ID: 11818253
Try the following code by added a javascript varible to identify whether need to call struts validation.

<script>
var isValidate = true;
</script>

<html:form action="/sample.do" method="post" onsubmit="return (isValidate)? validateForm(this) : true">
    ....
    <html:select property="country" onchange="isValidate=false; this.form.submit()">
    ....  
    </html:select>
    <html:submit property="insert" styleClass="button">submit</html:submit>
</html:form>

<html:javascript formName="sampleForm" method="validateForm" dynamicJavascript="true" staticJavascript="false"/>
0
 
LVL 2

Author Comment

by:sangeetha
ID: 11818370
Any idea why this Struts Form shows JS error (Object not found) :

<tr>

<td> Department </td>

<td colspan="3" >
  <html:select property="department" onchange="this.form.submit()">
    <html:option value="">Department A </html:option>
    <html:option value="">Department B </html:option>
  </html:select>
</td>

</tr>
0
 
LVL 39

Expert Comment

by:appari
ID: 11818482
try

<html:select property="department" onchange="document.f1.submit()">

change f1 to your form name

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:sangeetha
ID: 11818605
my fault...after changing the action name appropriately, i am able to run onchange="this.form.submit()" now.

boonleng,

i tried:

..  onsubmit="return (isValidate)? validateForm(this) : true">

but still the same problem.

could you please explain me  what is that validateForm(this)? Is that refers to Struts Form.

Thanks
0
 
LVL 14

Expert Comment

by:boonleng
ID: 11818726
You are using struts javascript validator or the struts ValidatorForm?
0
 
LVL 14

Expert Comment

by:boonleng
ID: 11818814
The above code i gave is based on struts javascript validator. If you are using the ValidatorForm the try the following code by adding a field as indicator in form bean and overwrite the validate() method.

JSP page:
<html:form action="/sample.do" method="post">
    <html:hidden property="isValidate"/>
    <html:select property="country" onchange="this.form['isValidate'].value=false; this.form.submit()">
    ....  
    </html:select>
    <html:submit property="insert" styleClass="button">submit</html:submit>
</html:form>

Form Bean:

private boolean isValidate = true;

// your getter & setter

public ActionErrors validate(ActionMapping arg0, HttpServletRequest arg1) {
    if (getIsValidate) {
        return super.validate(arg0, arg1);
    }
    return null;
}
0
 
LVL 2

Author Comment

by:sangeetha
ID: 11818846
I use public void validate(....) in my form-bean to validate the fields.
0
 
LVL 14

Accepted Solution

by:
boonleng earned 1000 total points
ID: 11818879
Ok... it seems like you didnt use the struts validator.
then it should be

public void validate(...) {
    if (getIsValidate) {
        //perform validation
    }
}
0
 
LVL 2

Author Comment

by:sangeetha
ID: 11818981
Ok...i will try that.
Could you please tell me why i get this error when i submit the form:

javax.servlet.jsp.JspException: Cannot find bean error in any scope
0
 
LVL 2

Author Comment

by:sangeetha
ID: 11819016
Ok..i got it....
Thanks.
0
 
LVL 4

Expert Comment

by:Oliver_Dornauf
ID: 11819030
sangeetha, first you should not submit a from if the field contents change. Use a button to submit a form.

To use the struts validator you have to specyfy the plugin in your web.xml.

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,    /WEB-INF/validation.xml"/>
</plug-in>

Take a closer look at the struts documentation:

http://struts.apache.org/userGuide/building_view.html#form_validation
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Check out what's been happening in the Experts Exchange community.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question