How to display error messages per each row in a datatable?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Star_Plus
Posted On:   Wednesday, May 7, 2008 09:43 PM

I'm displaying list of records using . In one of the columns I have an input field to enter username, which is a mandatory field. What I want to do is when you process the data if the username is not entered display the error message on the relevant row. Following are the code snipets..... In the html page: value="#{varTransferTask.toUser}"> In the java page: public String doCompleteAction() { if (toUser == null || toUser.trim().length() == 0) { getFacesContext().addMessage( getToUser().getClientId(getFacesContext()), new FacesMessage("To user is required")); } //   More>>

I'm displaying list of records using . In one of the columns I have an input field to enter username, which is a mandatory field.



What I want to do is when you process the data if the username is not entered display the error message on the relevant row.



Following are the code snipets.....



In the html page:



			





value="#{varTransferTask.toUser}">




In the java page:



			
public String doCompleteAction() {
if (toUser == null || toUser.trim().length() == 0) {
getFacesContext().addMessage(
getToUser().getClientId(getFacesContext()),
new FacesMessage("To user is required"));
}

// If there are errors return to the same page
if (getFacesContext().getMessages().hasNext()) {
return "";
}
// Otherwise go to next page
return "complete";
}


Problem is error messages are not getting displayed even though it's returning back to the same page. I know facesContext still contain those messages when it returns to the same page as they get displayed when I use tag.



Can anybody please help me how to resolve this?



Thanks!

   <<Less

Re: How to display error messages per each row in a datatable?

Posted By:   Star_Plus  
Posted On:   Thursday, May 8, 2008 08:41 PM

I found the answer to this..... :o)



I have actually forgotten to include the loop statement in the 'doCompleteAction()' method in the question description above. When I look at that, I realized that I've done a silly mistake....



I've looped through the collection of data used to display in the .... hence the 'toUser' input text id is not resolved properly. Instead when I loop through the datatable elements the problem is solved. I was able to display the error messages as I wanted, per each row in the table.



Here is the correct java code....




public String doCompleteAction() {
// Loop through the table data to perform validation
for (int i = 0; i < getTasksTable().getRowCount(); i++) {
// Set the index to current row in the table
getTasksTable().setRowIndex(i);

// Get the data from the current row
Task currentTask = (Task) getTasksTable().getRowData();
String toUser = currentTask.getToUser();

if (toUser == null || toUser.trim().length() == 0) {
getFacesContext().addMessage(
getToUser().getClientId(getFacesContext()),
new FacesMessage("To user is required"));
}
}

// If there are errors, return to the same page
if (getFacesContext().getMessages().hasNext()) {
return "";
}
// Otherwise, go to next page
return "complete";
}


In the above code:

getTasksTable() is the getter for the table and getToUser() is the getter for the input text within one of the columns.

About | Sitemap | Contact