Home arrow JavaScript arrow Page 4 - Validators: Concluding Remarks
JAVASCRIPT

Validators: Concluding Remarks


This article discusses two facets of the Struts-Validation framework. The first one deals with complex rules, while the second concerns information for creating custom rules that could be hooked up with the Validation framework.

Author Info:
By: A.P.Rajshekhar
Rating: 4 stars4 stars4 stars4 stars4 stars / 9
September 28, 2005
TABLE OF CONTENTS:
  1. · Validators: Concluding Remarks
  2. · Creating Custom Validators
  3. · Define the validation logic
  4. · Create an entry in the Validator-rules.xml

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Validators: Concluding Remarks - Create an entry in the Validator-rules.xml
(Page 4 of 4 )

The third and final step is to create an entry in the Validator-rules.xml. Now that the implementation part is over, let's see how to hook it up with the Validator framework. To do this I have to add this entry into the validator-rules.xml:

<validator name="phone"
                 classname="ap.mail.PhoneRules"
                 method="validatePhone"
                 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.phone">
</validator>

The above code has to be added after the <global> element of the Validator-rules.xml. The name attribute is given the value by which the custom rule would be used. The value is ‘phone’. Next the fully qualified Java class name of the class is supplied to the classname parameter. Next comes the method attribute. It expects the name of the method containing the validation logic. After that the parameters being passed have to be given in the methodParams attribute. The last attribute tells about the resource key of the message that would be used to display the message present in the Resource Bundle. Here the key is errors.phone.

This is how the custom rule is implemented. The above rule can be used as follows:

<field property="phone"
          depends="required,phone">
          <arg0 key="registration.phone" />
</field>

and add the corresponding error message to the application.properties file:

errors.phone={0} must be a valid phone number

Parting Thoughts

Though the framework makes validations (both server and client side) very easy to implement, the following points must be kept in mind:

  1. Server-side validation is recommended over client-side validation. The reasons for this are reliability and security. Server-side validation reduces the problems concerned with JavaScript based validation.

  2. If client-side validation is being used, keep a fall-back plan to validate the values entered in case JavaScript has been disabled or some other ‘unforeseen’ event happens.

  3. Last but not least try to use recurring rules, like certain pattern matching fields, by declaring them in <constants>.

This brings us to the end of the discussion on the Validator framework. This framework acts as a mediator between a Java application and JavaScript. It not only reduces development time but also provides a standard way to approach standard validation based problems. I hope the tutorial was helpful. Till next time.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials