Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Michael Kucinski
Greenhorn
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!
 
K. Tsang
Bartender
Posts: 3633
16
Firefox Browser Java Mac OS X
  • 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
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!