Sunday, 2 August 2009

Better validation messages in JSF

This week a collegue of mine wanted to enhance the form validation messages in a JSF application, but he was having problems getting a nice fieldname into the message. The default behavior of JSF is to display something like: j_id_id19: Validation Error: Value is required. This is Ok for a programmer, but an end-user gets kinda confused. There are several solutions for this problem:
  • Overwrite the default JSF messages in Messages.properties to not include the field name, but this will make the problem even worse.
  • Write a custom renderer for the messages, but this is about the same as amputating a leg when you've hit your toe against the bed.
  • Or we could just write a correct HTML form element that has, and this is the important bit, a label attribute, as this is what JSF will use in its default validation message. When this isn't present it will default to the id and when this id auto generated, as it usually is, will show something like j_id_id19.
So my collegue's problem was easily solved by adding label attributes to the fields of his form.

No comments:

Post a Comment