Solved

Struts form - validation

Posted on 2004-08-16
11
1,458 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A Short Story about the Best File Recovery Software – Acronis True Image 2017
Is your company's data protection keeping pace with virtualization? Here are 7 dynamic ways to adapt to rapid breakthroughs in technology.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now