
How does client side validation using validator framework work in struts ?

Hi friends,
Struts provides two files for validation.
1 : validator-rules.xml
2 : validation.xml
with the help of this example, you can do client side validation.
Step 1.
Code of jsp page :
`<html:form action="/bookSaveAaction" method="post" onsubmit="return validatebookForm(this);"`> <html:text property="bookId" size="30" maxlength="30"/> <html:text property="bookName" size="30" maxlength="30"/> <html:submit>Save</html:submit> <!-- Start tag of Validator Javascript Function--> <html:javascript formName="empForm"/> <!-- End tag of Validator Javascript Function--> </html:form> this tag is used for client side validation.
Step 2.
struts-config.xml is cofiguration file. It is used for action mapping
<action path="/bookSaveAaction" type="bookForm" name="AddressForm" scope="request" validate="true" input="/bookform.jsp"> <forward name="success" path="/success.jsp"/> </action> <form-beans> <form-bean name="bookForm" type="com.roseindia.form.bookForm" /> </form-beans>
Step 3. validator-rules.xml :
The validation definitions available for given application is defined by the validator-rules.xml files. The validator-rules.xml file acts as a template, defining all of the possible Validators that are available to an application.
validator-rules.xml File :
</global>
</form-validation>
<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.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
msg="errors.required"/>
</global>
</form-validation>
Step 4. validation.xml File : The validation.xml file is where you couple the individual Validators defined in the validator-rules.xml to components within your application. validation.xml File
<form-validation> <formset> <form name="bookForm"> <field property="bookId" depends="required"> <arg key="label.bookId"/> </field> <field property="bookName" depends="required"> <arg key="label.bookName"/> </field> </form> </formset> </form-validation>
The bookId and bookName are the required filed of the bookForm . So in the above configuration you can see we add for both bookId and bookName.
You can see depends="required" - "required" property is defind in validator-rules.xml.
In the resource bundle : application_resource.propertis file
label.bookId=bookId
label.bookName=bookName
Error messages used by the Validator
errors.required={0} is required.
Based on the validation.xml File configuration .
Error in jsp will be : Java Script message.
bookId is required.
bookName is required.
Thanks.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.