If you want to do any fancy styling of the bad field values, you could even call FacesMessages.addToControl() with your input fields component id. Note, that the component id will need to be fully qualified. So for instance if the component id is
usernameand it is inside a form with an id of
form:username) should do the trick. There is likely an easier way to do this now :)
Thanks everyone for the reply. This works great!
One question on this one. Will this solution work with fields under a ui:repeat?
Are you referring to the fully qualified component id? If so, then no out of the box it won't work with input fields in any repeating container such as a ui:repeat or h:dataTable. However, you can attempt to structure the componentId much the same way that the UIData does, namely to prepend the rowindex to the component id. So if your component is username, it is in a ui:repeat and the row index is 2, use
Not to derail, but has anyone in forum-land tinkered with using Drools for this kind of non-invariant validation?
Well, if you count login checking as form validation, then yes :-D
Come to think of it, maybe I'm not kidding - RuleBasedIdentity is a good example of a binary decision made using a quick Drools operation. I haven't done any performance testing, but AFAICT Drools is optimized for this sort of use case, so maybe a case could be made for using Drools for form validation. Certainly it's what I thought of when I read the OP's talk of business rules.
I have a similar problem, with a group of related checkboxes that the user must select one or more of. I've got the validation working in the action, but I'm having trouble getting a validation error message to appear near the checkboxes.
The checkboxes are generated from a list via a h:selectManyCheckbox so trying to hook the message to one of those would be kludgy and fragile. They're inside their own rich:simpleTogglePanel, but the addToControl(..) method seems uninterested in attaching to an element that isn't an input field.
Is there some way of defining a named anchor point that I can target with a RichFaces method, or will I have to fudge it by creating a dedicated attribute on my action to hold/display my error message?