Struts Validation Rules

Hi,

i am new to Struts framework. i have a Form,which contains 4 properties, say name, age, doj and sex.

in jsp all are Textfields. i need to implement js validation using struts validator for

1. sex should be either male or female

2. nameshould not contains any other chars than alphas.

3. Age value should be numeric without decimals.

4. doj (dateof joining) should be like mm-dd-yyyy

Pls Direct me thru Proper way.

Thanks all.

bhadra

[498 byte] By [bhadra.naira] at [2007-10-2 13:01:27]
# 1

Hi your form must extend the ValidatorActionForm

In your WEB-INF folder you place a validation.xml and a validator-rules .xml file.

In these files you use regular expressions to validate the input. Its working perfectly. In your jsp's you gotta add a <html:errors - tag.

If you want a couple of examples by mail let me know.>

SomeThingWeirda at 2007-7-13 10:22:15 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

hi,

i tried in this way

pls chk the code

register.jsp

-

<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<html:html>

<head>

<title>New Page 1</title>

</head>

<body>

<html:errors/>

<div align="center">

<center>

<table border="0" cellpadding="0" cellspacing="0" width="80%" height="550" bgcolor="#F2F3D6">

<tr>

<td width="100%" valign="top">

<div align="center">

<center>

<html:form action="/registerAction">

<table border="1" cellpadding="0" cellspacing="0" width="50%" bordercolor="#000080">

<tr>

<td width="50%"><bean:message key="label.firstName"/></td>

<td width="50%"><html:text property="fname" size="20"/></td>

</tr>

<tr>

<td width="50%"><bean:message key="label.lastName"/></td>

<td width="50%"><html:text property="lname" size="20"/></td>

</tr>

<tr>

<td width="50%">Salary</td>

<td width="50%"><html:text property="salary" size="20"/></td>

</tr>

<tr>

<td width="50%"> </td>

<td width="50%"><html:submit value="Submit Me" property="save"/></td>

</tr>

</table>

</html:form>

</center>

</div>

</td>

</tr>

</table>

</center>

</div>

</body>

</html:html>

register Form

package com.my.form;

import org.apache.struts.action.*;

import javax.servlet.http.*;

import org.apache.struts.validator.ValidatorForm;

public class RegisterForm extends ValidatorForm {

private String lname;

private String fname;

private String salary;

private String save;

public String getSalary() {

return salary;

}

public void setSalary(String salary) {

this.salary = salary;

}

public String getSave() {

return save;

}

public String getFname() {

return fname;

}

public String getLname() {

return lname;

}

public void setSave(String save) {

this.save = save;

}

public void setFname(String fname) {

this.fname = fname;

}

public void setLname(String lname) {

this.lname = lname;

}

/*public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {

return null;

}

public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {

lname = null;

fname = null;

salary = null;

save = null;

}*/

}

Action

--

package com.my.action;

import com.my.form.*;

import org.apache.struts.action.*;

import javax.servlet.http.*;

public class RegisterAction extends Action {

public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {

/**@todo: complete the business logic here, this is just a skeleton.*/

RegisterForm registerForm = (RegisterForm) actionForm;

return actionMapping.findForward("pass");

}

}

struts-config

--

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">

<struts-config>

<form-beans>

<form-bean name="registerForm" type="com.my.form.RegisterForm" />

</form-beans>

<action-mappings>

<action name="registerForm" path="/registerAction" type="com.my.action.RegisterAction">

<forward name="pass" path="/jsp/basic/register.jsp" />

</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>

</struts-config>

Validation.xml

-

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

<form-validation>

<formset>

<form name="registerForm">

<field property="fname" depends="required">

<arg0 key="label.firstName"/>

</field>

<field property="lname" depends="required">

<arg0 key="label.lastName"/>

</field>

</form>

</formset>

</form-validation>

validator-rules.xml

-

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

<form-validation>

<global>

<validator

name="required"

classname="org.apache.struts.validator.FieldChecks"

method="validateRequired"

methodparams="java.lang.Object,

org.apache.commons.validator.ValidatorAction,

org.apache.commons.validator.Field,

org.apache.struts.action.ActionErrors,

javax.servlet.http.HttpServletRequest"

msg="errors.required">

<javascript>

<![CDATA[

function validateRequired( form )

{

var fields = new array();

oRequired = new required();

for( x in oRequired )

{

var field = form.[oRequired[x][0]];

if( trim( field.value ).length == 0 )

{

alert("Field should not be empty");

return false;

}

}

return true;

}

function trim(s)

{

return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );

}

]]>

</javascript>

</validator>

</global>

</form-validation>

application resources/properties

-

[code]label.firstName=First Name

label.lastName=Last Name

#Error messages used by the Validator

errors.required={0} is required.

errors.minlength={0} can not be less than {1} characters.

errors.maxlength={0} can not be greater than {1} characters.

errors.invalid={0} is invalid.

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

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

[/code]

pls help

p()()ja

_Poojaa at 2007-7-13 10:22:15 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
hi,Anybody knows this?p()()ja
_Poojaa at 2007-7-13 10:22:15 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Write your own validation rules in the xml file and configure that in your validation.xml file....regardsShanu
mshanua at 2007-7-13 10:22:15 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Shanu,

I have done in the same way.( see the codes, i posted above).

I have a doubt. i am using Struts1.0. Did

<plug-in>

<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />

</plug-in>

support by the Struts version in its Struts-config file ?

if am removing the above line from config.xml, working without errors( but validation has not been done)

can i achieve this by using struts1.0?

p()()ja

_Poojaa at 2007-7-13 10:22:15 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Yes your remark is rite...Validation framework was implemented only in 1.1...Use the latest version and tryregardsShanu
mshanua at 2007-7-13 10:22:15 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
May be you can get more info from this link http://www.samspublishing.com/articles/article.asp?p=100587&rl=1regardsShanu
mshanua at 2007-7-13 10:22:15 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
shanu,Yes . i got............How can i use default validators like date, minlength, maxlength, Integer etcp(_)(_)ja
_Poojaa at 2007-7-13 10:22:15 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
hi,How do i use the default validastors for Date, email, creditcard Integer etc.......p(#)(#)ja
_Poojaa at 2007-7-13 10:22:15 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...