custom validators training

9
Custom Validators 

Upload: gayathrisuresh

Post on 02-Jun-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

8/10/2019 Custom Validators Training

http://slidepdf.com/reader/full/custom-validators-training 1/9

Custom Validators

8/10/2019 Custom Validators Training

http://slidepdf.com/reader/full/custom-validators-training 2/9

Struts Validator framework provides manyvalidation rules that can be used in the webapplications.The client-side validation in Struts is well known.Some of the available features:

requiredrequiredifvalidwhenminlengthmaxlengthmaskbyteshortintegerlongfloatDoublecreditCardemailurl

byteLocaleshortLocaleintegerLocalelongLocalefloatLocaledoubleLocaledateintRangelongRangefloatRangedoubleRange

8/10/2019 Custom Validators Training

http://slidepdf.com/reader/full/custom-validators-training 3/9

Edit validator-rules.xml and add the required code forthe 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 validatorcreated

Error message issuedin the browser

8/10/2019 Custom Validators Training

http://slidepdf.com/reader/full/custom-validators-training 4/9

8/10/2019 Custom Validators Training

http://slidepdf.com/reader/full/custom-validators-training 5/9

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>

8/10/2019 Custom Validators Training

http://slidepdf.com/reader/full/custom-validators-training 6/9

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-propertyproperty="pathnames"value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>

</plug-in>

8/10/2019 Custom Validators Training

http://slidepdf.com/reader/full/custom-validators-training 7/9

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 ValidatorFramework validates the form.</p>

<p><html:errors/></p> Continued …

8/10/2019 Custom Validators Training

http://slidepdf.com/reader/full/custom-validators-training 8/9

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

8/10/2019 Custom Validators Training

http://slidepdf.com/reader/full/custom-validators-training 9/9

Create the Java Class for the AdminFormCreate the success.jsp

Create the AdminAction.java