How to validate datatable in JSF
I have two JSF <h:selectOneMenu>
components. One called category another is subcategory.
If I select category, then subcategory changes dynamically. When I click search command button, then it displays a <h:dataTable>
with boolean checkboxes.
The problem is: if none of the checkboxes is selected, then I need to show the error message like "Please select any" and the same error message has to come if there is no data in the data table after 开发者_JAVA技巧pressing search button. Please suggest a solution.
if none of the checkboxes is selected, then I need to show the error message like "Please select any".
That depends on how you're grouping the checkboxes and collecting the checked items. I bet that you're using a Map<RowIdType, Boolean>
to collect them. In that case, you just need to count the amount of checked items in the bean action method. If this equals to 0
, then add a FacesMessage
like follows:
facesContext.addMessage(null, new FacesMessage("Please select any"));
and display it in a <h:messages />
the usual way.
and the same error message has to come if there is no data in the data table after pressing search button.
You can do it the same way. In the bean action method, check how many rows are returned and if this equals to 0
, then add a FacesMessage
.
精彩评论