This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes Form validation in struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Form validation in struts" Watch "Form validation in struts" New topic
Author

Form validation in struts

Jas Oberai
Ranch Hand

Joined: Jan 16, 2005
Posts: 231
Hi Friends,
I am tryin to validate my form in struts using Validator form.First question,my form class extends ActionForm,but they are saying the form class should extend ValidatorForm,which is correct???

I made my form class extend the ValidatorForm class,and wrote all the validation components,and its validating but no pop-up box is coming up.And I have disabled my pop-up blocked too.Here's are my parts:

validation.xml:

Struts-config.xml:

voteForm class:


addVote.jsp


Please help friends....


SCJP 1.4 (88%)<br />SCWCD 1.4 (88%)
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
First question,my form class extends ActionForm,but they are saying the form class should extend ValidatorForm,which is correct???


If you're going to use the Struts Validation Framework, your Action Form must extend ValidatorForm.

Regarding why your client-side validation isn't working, I'd try changing:

<html:form action="/addvote" method="post" onsubmit="return validatevoteForm(this);" >

To:

<html:form action="/addvote" method="post" onsubmit="return validateVoteForm(this);" >

If you use your browser's "view source" function to view the actual content of the page, you can see the JavaScript code that Struts put in the page. You should be able to see a "validateVoteForm" JavaScript method.


Merrill
Consultant, Sima Solutions
Jas Oberai
Ranch Hand

Joined: Jan 16, 2005
Posts: 231
That worked..thanks merill..have a nice day!!!
Anju sethi
Ranch Hand

Joined: Dec 26, 2005
Posts: 91
Hi Friends,

I am also trying to validate my login form in struts using Validator form.

I made my form class extend the ValidatorForm class,and wrote all the validation components,and its validating but no pop-up box is coming up.
And I have disabled my pop-up blocked too.Here's are my parts:

validation.xml:

code:
--------------------------------------------------------------------------------

<formset>
<!-- An example form -->
<form name="LoginForm">
<field property="username" depends="required">
<arg key="tlms.logonForm.username" />
</field>
<field property="password" depends="required,mask">
<arg key="tlms.logonForm.password" />
<var>
<var-name>mask</var-name>
<var-value>^[0-9a-zA-Z]*$</var-value>
</var>
</field>
</form>
</formset>
--------------------------------------------------------------------------------


Struts-config.xml:

code:
--------------------------------------------------------------------------------

<action-mappings>
<action path = "/LoginAction"
type = "com.hsc.tlms.action.LoginAction"
name = "LoginForm"
scope = "request"
validate="true"
input = "/JSP/Login.jsp"
>
<forward name="LoginSuccess" path="/HomeAction.do" />
</action>
</action-mappings>
.... .... ....

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

--------------------------------------------------------------------------------


LoginForm class:

code:
--------------------------------------------------------------------------------

public class LoginForm extends ValidatorForm{ // getters and setters}

--------------------------------------------------------------------------------



Login.jsp

code:
--------------------------------------------------------------------------------

<html:form action="LoginAction.do" focus="userId" onsubmit="return validateLoginForm(this)"><table>
<tr> ... ... ... </tr>
</table>
<html:submit property="submit" value="Submit" />
</html:form>
</body>
</html:html>

And <html:javascript formName="LoginForm" /> in the <head> of the jsp page.

Please help


thanks,<br />Anju Sethi
Anju sethi
Ranch Hand

Joined: Dec 26, 2005
Posts: 91
This is my jsp page

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>

<html:html locale="true">
<head>
<html:javascript formName="LoginForm" />
<link rel="stylesheet" href="global.css" type="text/css" >
</head>

<body bgcolor="#eeeeee">
<table align="center">
<tbody>
<tr>
<td align = "center">
<h2><bean:message key="tlms.jsp.loginform.heading"/></h2>
</td>
</tr>

<tr>
<td align = "center">
<b><html:errors property="loginForm"/></b>
</td>
</tr>
</tbody>
</table>

<html:form action="LoginAction.do" focus="userId" onsubmit="return validateLoginForm(this)">
<table align="center">
<tbody>
<tr>
<td align="right"><span class="required">*</span>

<bean:message key="tlms.loginform.userid" /></td>

<td align="left">

<html:text property="userId" /></td>

<html:errors property="userId"/>
</tr>

<tr>
<td align="right"><span class="required">*</span>

<bean:message key="tlms.loginform.password" /></td>

<td align="left">

<html assword property="password" /></td>

<html:errors property="password"/>


</tr>

<tr>
<td> </td>

<td><html:link forward="ChangePassword">change password</html:link></td>
</tr>

<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td align="center" colspan="2"><html:submit property="submit" value="Submit" />
<html:reset>Reset</html:reset></td>
</tr>
</tbody>
</table>
</html:form>
</body>
</html:html>
Anju sethi
Ranch Hand

Joined: Dec 26, 2005
Posts: 91
Please help
 
Consider Paul's rocket mass heater.
 
subject: Form validation in struts
 
Similar Threads
Automatic Validation does not work
Another "Validator Plugin" Problem
Error 'required' is undefined
Struts Validator Configuration
Validator Problem