Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger

Is it possible to create a tag in JSF that acts as a stand alone validator?

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been asked to write a custom component in JSF 2. The idea is the tag would take in several date attributes and do a sort of validation on the passed values.

I've written tags before, but in every other case the tags have contain some UIComponent that I can register a validator with. In this case, I can't think of a way to register a validator on the passed components. This is the awful component that I cooked up so far:

As you can see, I'm using EL to pass my component to a validator and letting the h:messages handle the exceptions I'm throwing. But I feel like there has to be a better way to do this. I guess this shows my lack of experience with JSF. I appreciate the help!
Posts: 3648
Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you need to do the validate in the client side or can be server side? If server side there's JSF f:validator tag and point to your custom date validator. If client side javascript do some kind on submit check.

It's not recommended to write your own tags in JSF. If the standard JSF tags don't have, there's other libraries you can consider eg Primeface, ICEfaces, etc
Saloon Keeper
Posts: 22122
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is syntactic validation (more than one decimal point in a number, text in a number, malformed date, and so forth), and then there is semantic validation (end date >= begin date, double password entry/check, etc.)

The JSF validators concentrate on syntactic validation, not semantic validation, considering semantic validation to be essentially part of the business logic, and thus more properly implemented in the action methods than in the View template. What I normally do in such cases is placed that code at the head of the action method and before the real business logic gets invoked, I do all my semantic validations, put messages (if needed) to JSF Messages, then short-circuit the business logic by immediately returning "null" to redisplay the page with the validation failures in the JSF Messages collection.

If you were to write a custom binary tag - and I don't recommend it for more reasons than just the above - you could take your cue from other decorator tags, which tend to either expect to be nested within the tag or use a tag id reference(s).

You wouldn't succeed in getting your validator example to work in any event. The Model doesn't get updated until/unless all validations have succeeded, so you'd be looking at the "before" values of the data being validated, not the current values in the form being submitted.
Being a smart alec beats the alternative. This tiny ad knows what I'm talking about:
Devious Experiments for a Truly Passive Greenhouse!
    Bookmark Topic Watch Topic
  • New Topic