Solved

Struts form - validation

Posted on 2004-08-16
11
1,466 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
Industry Leaders: 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 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

Industry Leaders: 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!

Question has a verified solution.

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

This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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