This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSF and the fly likes How to identify h:message within a datatable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "How to identify h:message within a datatable?" Watch "How to identify h:message within a datatable?" New topic
Author

How to identify h:message within a datatable?

Chris Gabel
Greenhorn

Joined: Jan 17, 2011
Posts: 7
I'm trying to place an error message beside a field in a datatable that it applies to, but I'm having trouble identifying it in the backing bean. I have an area on the top of my form for non-field-specific errors/info, but when the error is specific to a field (i.e. "email") I want the message to appear there.

This is my XHTML: (I've removed other non-relevant sections of the form for clarity)


This is my backing bean:

When I run the code, I get the error message:

[05/06/13 13:50:28:371 PDT] 00000026 RenderRespons W There are some unhandled
FacesMessages, this means not every FacesMessage had a chance to be rendered.
These unhandled FacesMessages are:
- Email address cannot be left blank when selecting email delivery.


Does anyone know what I'm doing wrong? I have a feeling the clientId of the message is not being set properly.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2229
    
    7

The h:outputText id attribute should match the h:message for attribute in order to display the error message. And you should not need an id attribute for h:message

K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Chris Gabel
Greenhorn

Joined: Jan 17, 2011
Posts: 7
Thanks for the suggestion, K. Tsang, but it didn't work.

I changed the XHTML to be:


... and the bean code to be:


Am I missing something?
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2229
    
    7

Did you have a <h:messages> tag in your XHTML file? Because the FaceContext.getCurrentInstance().addMessage() I think adds to this <h:messages> area.

Ideally you should use a Validator to validate your inputs. Also the Validator will definitely put the error message next to the invalid input. Yet it can get tricky with data tables, especially editable ones.

Further inspection, why is you email a outputText? There's nothing to validate.

If your email is indeed an inputText then your table would need an id attribute. I believe the id can't be found so the FaceContext doesn't know what to do.

The new id will then look like "DeliveryOptionsForm:<table id>:<row number>:emailLabel". Yes the row number is another thing you missed because it's in a table. Such attributes can be confirmed using tools like Firefox firebug.
Chris Gabel
Greenhorn

Joined: Jan 17, 2011
Posts: 7
I have a <h:messages> tag earlier on the page with the globalOnly="true" parameter. I have other messages which need to be displayed here. The ones specific to the Email field need to display next to the field they represent.

My page has a <h:outputText> tag to render the label for the field (Email) and the <h:inputText> field for the email is right below it. I want the error message to appear next to the Email label. I use a <f:validateRegex> tag to make sure it's a valid email, but that check passes with a blank email field. I can't use a "required=true" parameter because there are many Email fields on the screen, and I only want it to validate if the "Email" option is selected.

I'm using a dataTable to display 19 different sets of product types and their associated delivery options. The user can choose "MAILBOX", "EMAIL", or "NONE". When they choose email, an email label and input field appear (controlled by CSS).

How does the backing bean know which row number the error is occurring in? Is that passed as a parameter to the backing bean?
Chris Gabel
Greenhorn

Joined: Jan 17, 2011
Posts: 7
ACK! I feel stupid. I solved my own problem by putting the logic of the "required" parameter on the value of the drop down menu. That way it's all client side validation, and the backing bean is never called.



Thanks for the nudge in the right direction, K. Tsang.
 
 
subject: How to identify h:message within a datatable?
 
Similar Threads
Display a Popup instead of an error-message at conversionError
How to display "message sent" after to invoke commandButton ?
Problem with h:message. Someoone Please help
Why can't I see my validation errors?
Problem with error messages not displaying