Solved

Struts form - validation

Posted on 2004-08-16
11
1,464 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
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 250 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

The advancement in technology has been a great source of betterment and empowerment for the human race, Nevertheless, this is not to say that technology doesn’t have any problems. We are bombarded with constant distractions, whether as an overload o…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

830 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