Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Form validation in struts

 
Jas Oberai
Ranch Hand
Posts: 231
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jas Oberai
Ranch Hand
Posts: 231
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That worked..thanks merill..have a nice day!!!
 
Anju sethi
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anju sethi
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic