aspose file tools*
The moose likes Struts and the fly likes problem in  Struts Validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "problem in  Struts Validation" Watch "problem in  Struts Validation" New topic
Author

problem in Struts Validation

Hariharan.S
Greenhorn

Joined: May 25, 2005
Posts: 7
Hi,

I am trying to use Struts Validation. But I didn�t get , Please help me to solve it.
I am getting error message as validateLoginForm is not defined.

Here are the files I am using

Login.jsp

<html:form action="/login" method="post" onsubmit="validateLoginForm();">
.
.
.
.
 <html:text property="userName" styleClass="txtstyle" size="20" maxlength="15"/>

 <html assword property="password" styleClass="txtstyle" size="20" maxlength="15"/>

<html:javascript formName="loginForm"/>

</html:form>

</body>
</html:html>


struts-config.xml

<form-bean name="loginForm" type="com.newhorizon.vrm.web.form.common.LoginForm" />


<action
attribute="loginForm"
input="/jsp/common/login.jsp"
name="loginForm"
path="/login"
validate=�true�
type="com.newhorizon.vrm.web.action.common.LoginAction">
<forward name="loginfailure" path="/jsp/common/login.jsp" />
<forward name="loginsuccess" path="/jsp/common/home.jsp" />
</action>

<!-- Message Resources -->
<message-resources parameter="com.newhorizon.vrm.web.ApplicationResources" />

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



Validation.xml

<!-- Login form Validation-->
<!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//
DTD Commons Validator Rules Configuration 1.2//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_2_0.dtd">

<form-validation>


<formset>
<form name="loginForm">
<field property="userName" depends="required"> <arg0 key="userName"/>
</field>

<field property="password" depends="required,mask">
<arg0 key="password"/>
<var>
<var-name>mask</var-name>
<var-value>^[0-9a-zA-Z]*$</var-value>
</var>
</field>
</form>
</formset>

</form-validation>


LoginForm.java

import org.apache.struts.validator.*;
public class LoginForm extends ValidatorForm {
private String userName = null;
private String password = null;

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}



}

LoginAction.java

public class LoginAction extends Action {

public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)throws IOException, ServletException
{
System.out.println("insideerrors : " );
LoginForm loginform =(LoginForm) form;

String username = loginform.getUserName();
String password = loginform.getPassword();

if(username.length()!=0)
{
String res =username+"<br>"+password;
javax.servlet.http.HttpSession session = request.getSession();
session.setAttribute("result",res);
return mapping.findForward("loginsuccess");
}
else
{
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("errors.global"));
saveErrors(request,errors);
System.out.println("errors : " + errors);
return(mapping.findForward("loginfailure"));
}
}
}


ApplicationResources.properties

errors.nulluserName='enter your ID
errors.nullpassword=enter your PASSWORD
errors.global=DOES NOT MATCH
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Your onsubmit should be

onsubmit="return validateLoginForm(this);"

You use return so that if the form is not valid, the form does not submit (returns false). You use this as an argument to the validateLoginForm function because it takes the form itself as a parameter.
[ May 18, 2006: Message edited by: Gregg Bolinger ]

GenRocket - Experts at Building Test Data
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

By the way, your display name does not completely comply with our Naming Policy. Initials are ok for the first half (first name) of your name, but not the second half (last name).

Please see what you can do about that.

Thanks.
Hariharan.S
Greenhorn

Joined: May 25, 2005
Posts: 7
hi,

yeah now i am getting. But i got alert as ' is required' . I want to display like 'UserName is required' 'Password is required'. My ApplicationResources.properties file is here..


errors.invalid={0} is invalid.

errors.maxlength=pincode can not be greater than 6 characters.

errors.minlength=password can not be less than 6 characters.

errors.range={0} is not in the range {1} through {2}.

errors.required={0} is required.

errors.byte={0} must be an byte.

errors.date={0} is not a date.

errors.double={0} must be an double.

errors.float={0} must be an float.

errors.integer={0} must be an integer.

errors.long={0} must be an long.

errors.short={0} must be an short.

errors.creditcard={0} is not a valid credit card number.

errors.email={0} is an invalid e-mail address.

errors.nulluserName=ID
errors.nullpassword=PASSWORD
Hariharan.S
Greenhorn

Joined: May 25, 2005
Posts: 7
hi,

i got it by assigning a string to the key in AR.properties.

like : userName=UserName

Is this approach good or any other way.
Hariharan.S
Greenhorn

Joined: May 25, 2005
Posts: 7
i am getting alert msg for 'password is required'. But i am not getting alert msg for 'password minlength'. But if i have specified <html:errors/> in the jsp page, i am getting proper error message in the form of text. Why this problem occurs?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in Struts Validation