JSF Central - Metadata based validation for JavaServer Faces – Part 1
JSF Central

 
 Home 
 
 Products 
 
 Articles & Books 
 
 Resources 
Metadata based validation for JavaServer Faces
 
Metadata based validation for JavaServer Faces – Part 1
by Gerhard Petracek
31 Mar 2009 07:30 EST

In the first article of this series, Gerhard Petracek introduces MyFaces ExtVal, a JSF-centric validation platform that provides advanced features not yet available in other JSF validation frameworks.


Validation is an important part of every business application. Therefore JavaServer Faces (JSF) provides a specific phase for it that covers the needs of many web applications. However as time passed, new features and specifications were introduced by the Java Community Process (JCP). One feature that became an important part of Java software development was metadata via annotations. For instance, the Java Persistence API (JPA) uses annotations to simplify development. With annotations, information is attached to the artifact it belongs to. This article illustrates how they can be applied to validation with JSF.

In JSF, you have to define validation logic for every input component to be validated. In several cases this means you must violate the "Don't Repeat Yourself" (DRY) principle. A simple example is a property of an entity that is specified as required in JPA. Even though this information is already available in the model, you also have to specify that the input component bound to the mentioned property is required, by setting the required attribute to true. If the model changes, you have to go though all pages of your JSF application and change the corresponding components. You can resolve such restrictions with Apache MyFaces Extensions Validator framework (aka MyFaces ExtVal).

2008 - A new project was born

The root of MyFaces ExtVal was a feasibility study called "simple enhanced validation with extensible nature" (sev-en). It was started in Q1 2008 and aimed to provide transparent validation based on JPA annotations, as well as metadata based cross-component validation, in a clean manner. In Q2 2008, sev-en continued at Apache MyFaces as an extensions sub-project called Extensions-Validator or for short, ExtVal. Before the first release on December 12th, 2008, it was used in several real-world JSF applications, and the donated project evolved to a highly extensible and customizable framework with advanced validation features.

Yet another validation framework for JSF?

MyFaces ExtVal is not just "yet another validation framework for JSF." While the Seam integration of Hibernate Validator is especially well-known for its metadata based validation for JSF, a side-by-side comparison of MyFaces ExtVal and Seam shows that MyFaces ExtVal offers several advanced mechanisms that are not yet available in the current Seam integration of Hibernate Validator or other JSF validation frameworks. Besides advanced validation features, it offers a solid platform for custom add-ons. The "secure required" add-on illustrates that MyFaces ExtVal is also able to solve some known restrictions of JSF. Because of several extension points, it is possible to add additional features very easily. MyFaces ExtVal also provides several features out of the box. Most of them are optional, and you can re-use them for custom validations. Since they are not required, you can also use the ExtVal-Core to build your own infrastructure with your own features. However, this article primarily introduces MyFaces ExtVal and its ready-to-use features.

Hello MyFaces ExtVal

Two sample applications are shipped with MyFaces ExtVal. The Hello World application is based on the default Hello World example of MyFaces itself. The following source code and page fragments illustrate validation based on JPA annotations. As you can see, MyFaces ExtVal doesn't introduce requirements to the annotations. You can validate any information of an existing set of annotations without adjusting the annotations. That's a central principle of the framework! It allows you to use any custom and/or existing annotation in any layer of your application without introducing ExtVal as a dependency outside the view layer. ExtVal just validates the annotations that are present on a property bound to an input component.

  Figure 1 shows a simple entity. In this case the attributes nullable and length contain the important information for validation.
Figure 1 shows a simple entity. In this case the attributes nullable and length contain the important information for validation.

Within the page there are neither additional validation details nor special tags. Figure 2 shows the important page fragment.

  Figure 2. The page fragment
Figure 2. The page fragment

The rendered page is illustrated in figure 3.

  Figure 3. The rendered page
Figure 3. The rendered page

If we look at the rendered HTML code fragment in figure 4, we find correct values in the maxlength attributes. In addition to server-side validation this feature allows restrictions on the client side. In this case, MyFaces ExtVal also follows the DRY principle.

  Figure 4. Fragment of the rendered HTML code
Figure 4. Fragment of the rendered HTML code

MyFaces ExtVal is also able to use the client-side validation mechanisms of a component library like MyFaces Trinidad. Moreover, a conceptual implementation of a custom client-side validation extension is available here. If validation fails, the standard validation error messages of JSF are displayed. As you can see in figure 5, the correct label texts are used in the error messages.

  Figure 5. Failed validation
Figure 5. Failed validation

For custom validation messages, you can define custom JSF messages. You can also provide special error messages for JPA constraints. Beyond that, MyFaces ExtVal offers a message resolver mechanism that allows message-resolving from an alternative source such as a database. The default implementation uses the resource bundle format as a message source format, but you can provide a custom resolver that uses other message sources.

Advanced validation features

MyFaces ExtVal provides many more features than we've discussed. The following list shows the most important ones:

  • Custom meta-data based validation
  • Annotation based cross-validation
  • Internationalized validation error messages
  • Validation exception interception
  • Component initialization based on metadata
  • Client-side validation based on metadata
  • Dependency injection support
  • Conditional validation
  • Extensible and pluggable validation platform

Some of these features will be described in the following parts of this series.

Summary

This first article provides a short introduction to MyFaces ExtVal, a JSF-centric validation platform that provides advanced features that are not yet available in other JSF validation frameworks. MyFaces ExtVal smoothly integrates into existing technologies and allows you to follow the Ease of Development (EoD) paradigm as well as the "Don't Repeat Yourself" (DRY) principle. In the next part I'll introduce some ready to use annotations and the metadata based cross-validation feature of MyFaces ExtVal.

If you have questions or proposals, you are welcome to send them to the MyFaces mailing list. IRIAN Solutions offers trainings and first class support for MyFaces ExtVal as well as MyFaces in general. An introduction to client-side validation with MyFaces Trinidad based on metadata will be part of the MyFaces Trinidad workshop at JSF Days Vienna 2009.

Resources



RSS feed(all feeds)

The Editor's Desk
Podcasts
Inside Facelets
In the Trenches

Site version 1.83  Report web site problems

Copyright (C) 2003-2014 Virtua, Inc. All Rights Reserved. Java, JavaServer Faces, and all Java-based marks are trademarks or registered trademarks of Oracle Corporation. in the United States and other countries. Virtua, Inc. is independent of Oracle Corporation. All other trademarks are the sole property of their respective owners.