It's not a secret anymore!
The moose likes Struts and the fly likes Validate() method of ActionForm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Validate() method of ActionForm" Watch "Validate() method of ActionForm" New topic

Validate() method of ActionForm

Manu Ramakrishnan

Joined: Jun 07, 2003
Posts: 10
Can anybody please help ? I am using WebSphere 4.0 Application server.
I am using the validate() method for client side validation. I am getting redirected to the input Page after validation errors. But the Error Messages which are coded in not shown in the page
<%@ page language= "java" session="true" %>
<%@ taglib uri="/WEB-INF/lib/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/lib/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/lib/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/lib/struts-validator.tld" prefix="validator" %>
<body bgcolor="#E2E2E2">
<font face="Trebuchet MS" size=1>
<html:form action="/change_password">
            CHANGE AUTHENICATION PASSWORD</b></h4><br><br>
<html:hidden property="userId" value='<%=(String)session.getAttribute("userId")%>'/>
<table border="0" width="50%" height="127" cellspacing="1">
<td width="50%" height="26" align="right">User</td>
<td width="50%" height="26" align="left">
<input type="text" name="userName" size="20" value='<%=(String)session.getAttribute("userName")%>' style="background-color: #C0C0C0" readonly="true">
<td width="50%" height="26" align="right">Old password</td>
<td width="50%" height="26" align="left">
<html assword property="oldpwd" size="12" maxlength="8"/>
<td width="50%" height="26" align="right">New password</td>
<td width="50%" height="26" align="left">
<html assword property="newpwd" size='12' maxlength='8'/>
<td width="50%" height="26" align="right">Re-type new password</td>
<td width="508%" height="26" align="left">
<html assword property="rnewpwd" size="12" maxlength="8"/>
<td colspan="2" height="26"align="center">
<td colspan="2" height="26" align="center">
<html:submit property = "change" value="Change Password"/>
<html:reset value="Reset"/>

Validate method
public ActionErrors validate(ActionMapping amapping, HttpServletRequest request)
ActionErrors errors=new ActionErrors();
if ((oldpwd==null) || (oldpwd.length() != 8))
errors.add("oldpwd", new ActionError("error.changepwd.oldpwd"));
if ((newpwd==null) || (newpwd.length() != 8))
errors.add("newpwd", new ActionError("error.changepwd.newpwd"));
if ((rnewpwd==null) || (rnewpwd.length() != 8))
errors.add("rnewpwd", new ActionError("error.changepwd.rnewpwd"));
if (!newpwd.equals(rnewpwd))
errors.add("notmatch", new ActionError("error.changepwd.notmatch"));
return errors;
errors.header=<h3>Errors :</h3><UL>
error.changepwd.oldpwd=<li>Old password length is invalid</li>
error.changepwd.newpwd=<li>New password length is invalid</li>
error.changepwd.rnewpwd=<li>Retyped new password length is invalid</li>
error.changepwd.notmatch=<li>New password and Re-typed new password do not match</li>
[ July 02, 2003: Message edited by: Manu Ramakrishnan ]
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
First thing for your info, this is a server side validation. Client side validation can only be done via Javascript.
To show your error messages add the following code to your page,e.g.
<html:errors property="changepwd.oldpwd"/>
On this location the error message now appears
I agree. Here's the link:
subject: Validate() method of ActionForm
jQuery in Action, 3rd edition