Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Frits Walraven
  • Himai Minh

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

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 24179
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
Enjoy the full beauty of the english language. Embedded in this tiny ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic