Solved

Struts form - validation

Posted on 2004-08-16
11
1,462 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
tomcat users xml 7 121
java cigar challenge 58 212
build fail in maven project 11 186
print map entry 34 69
Each year, investment in cloud platforms grows more than 20% (https://www.immun.io/hubfs/Immunio_2016/Content/Marketing/Cloud-Security-Report-2016.pdf?submissionGuid=a8d80a00-6fee-4b85-81db-a4e28f681762) as an increasing number of companies begin to…
When you have clients or friends from around the world, it becomes a challenge to arrange a meeting or effectively manage your time. This is where Outlook's capability to show 2 time zones in one calendar comes in handy.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
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…

810 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