开发者

Grouping JSF error messages

开发者 https://www.devze.com 2023-01-18 20:43 出处:网络
I have to break the date input in a form into 3 different parts, the day, month and year. At the moment whenever there is an error in all three fields开发者_如何学C, 3 error messages are displayed. I\

I have to break the date input in a form into 3 different parts, the day, month and year. At the moment whenever there is an error in all three fields开发者_如何学C, 3 error messages are displayed. I'm wondering if there is a way to group these error messages? So if one or more fields has an error only one error message will be displayed. Alternatively is there away to check the content of the message and i can render messages only if the other messages are null

Thanks for your time.


The standard JSF API does't provide this functionality. You've to look for another solution. I at least see two basic solutions:

  1. Implement a custom converter/validator which intercepts on all the three fields and just don't convert/validate when at least one field of the group has an error. You can find an example in this answer.

  2. Group all messages in some custom structure yourself in a PhaseListener during beforePhase of RENDER_RESPONSE. This however decouples your code from default JSF message handling which may lead to maintenance trouble in long term.

More detail depends on the functional requirements and what you've as far, which is yet unclear. Leave a comment with more detail whenever you need more detail.


An easy way is to have a single validator for all the three date fields (month,day and year). And inside this validator show a single context message.

While in JSF page add a fourth input hidden field, along with a <h:message /> to show error messages about the errors in date there. Using the 'id' of this input hidden field in the validator for error messages.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号