Você está na página 1de 9

Custom Validators

Struts Validator framework provides many


validation rules that can be used in the web
applications.
The client-side validation in Struts is well known.
Some of the available features:
required
requiredif
validwhen
minlength
maxlength
mask
byte
short
integer
long
float
Double
creditCard
email
url
byteLocale
shortLocal
e
integerLoc
ale
longLocale
floatLocale
doubleLoc
ale
date
intRange
longRange
floatRange
doubleRan
ge


Edit validator-rules.xml and add the required code for
the new rule.


<validator name="matchname"
classname="org.apache.struts.validator.FieldChecks"
method="validateName"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"

msg="errors.name">


Creating a new Validator
matchname is
the new validator
created
Error message issued
in the browser
<javascript><![CDATA[
function validateName(form) {
var isValid = true;
var focusField = null;
var i = 0;
var fields = new Array();
var omatchName= eval('new ' + jcv_retrieveFormName(form)
+ '_matchname() ');
for (var x in omatchName) {
if (!jcv_verifyArrayElement(x, omatchName[x])) {
continue;
}
var field = form[omatchName[x][0]];

if (!jcv_isFieldPresent(field)) {
fields[i++] = omatchName[x][1];
isValid=false;
} else if (field.value != "administrator") {
fields[i++]=omatchName[x][1];
isValid=false;
}
}
if (fields.length > 0) {
jcv_handleErrors(fields, focusField);
}
return isValid;
}
]]>
</javascript>
</validator>
Test the value
entered in the
browser
Edit validation.xml and add:
<!-- Name form Validation-->
<form-validation>
<formset>
<form name="AdminForm">
<field property="name"
depends="matchname">
<arg0 key="AddressForm.name"/>
</field>
</form>
</formset>
</form-validation>
Edit struts-configuration.xml and add:
form-bean name="AdminForm" type="test.AdminForm"/>
<action
path="/AdminFormValidation"
type="test.AdminForm"
name="AdminForm"
scope="request"
validate="true"
input="admin.jsp">
<forward name="success" path="success.jsp"/>
</action>

<message-resources parameter="resources/application"/>

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
Create a JSP file
<%@ taglib uri="struts-bean.tld" prefix="bean" %>
<%@ taglib uri="struts-html.tld" prefix="html" %>

<html:html>
<head>
<title>Administrator Test</title>
<html:base/>
</head>
<body bgcolor="white">
<html:form action="/AdminFormValidation" method="post"
onsubmit="return validateAdminForm(this);">

<div align="left">
<p>
This application shows the use of Struts Validator.<br>
The following form contains fields that are processed by Struts
Validator.<br>
Fill in the form and see how JavaScript generated by Validator
Framework validates the form.
</p>

<p>
<html:errors/>
</p>
Continued
<table>
<tr>
<td align="right">
<b>Name</b>
</td>
<td align="left">
<html:text property="name" size="30" maxlength="30"/>
</td>
</tr>
<tr>
<td align="right">
<html:submit>Save</html:submit>
</td>
<td align="left">
<html:cancel>Cancel</html:cancel>
</td>
</tr>
</table>
</div>

<!-- Begin Validator Javascript Function-->
<html:javascript formName="AddressForm"/>
<!-- End of Validator Javascript Function-->
</html:form>
</body>

</html:html>
Create the Java Class for the AdminForm
Create the success.jsp
Create the AdminAction.java

Você também pode gostar