Problem with validation on an ajax supported h:selectOneMenu
kairos Jun 27, 2009 8:57 AMI want to implement a validation on an onchange event of a selectOneMenu. This menu is part of a h:datatable and it must be guaranted that the selected value is not already selected anywhere else in the table. I think this is a situation which might happen quit often. But I am not able to solve it.
This is part of my xhtml:
<h:dataTable id="exTable" var="_exWrapper" value="#{exercises}"> <h:column> <f:facet name="header">#{messages.exName}</f:facet> <h:selectOneMenu id="selectExercises" value="#{_exWrapper.exercise}" required="true" converter="#{exerciseConverter}" validator="#{myValidator.validate}" validatorMessage="#{messages.dublicateExercise}"> <s:selectItems var="exercise" value="#{exerciseManager.exercises}" label="#{exercise.name}" noSelectionLabel="#{messages.pleaseSelect}"/> <a4j:support event="onchange" reRender="selectExercise, messagesField" ajaxSingle="true"/> </h:selectOneMenu> </h:column> ....
I tried the validator approach:
@Name("myValidator") @BypassInterceptors @org.jboss.seam.annotations.faces.Validator public class MyGymExerciseValidator implements Validator { public void validate(FacesContext arg0, UIComponent arg1, Object arg2) throws ValidatorException { ExWrapperHome home = (ExWrapperHome) Component.getInstance("exWrapperHome"); if (!myGymHome.checkExercise((Exercise) arg2)) { throw new ValidatorException(new FacesMessage()); } } }
I created an empty FacesMessage because I didn't succeed in getting access to the resource boundle. Therefore I use the validatorMessage.
Anything works: the validator is called, the exception is thrown but: the problem is that neither message is displayed nor the selectonemenu is rerendered.
But I see the following message in the console:
08:29:26,203 INFO [lifecycle] WARNING: FacesMessage(s) have been enqueued, but may not have been displayed. sourceId=myForm:exTable:3:selectExercises[severity=(ERROR 2), summary=(the message), detail=(the message)]
The messages panel is defined in the template.xhtml:
<rich:layout> <rich:layoutPanel position="top" width="100%"> <a4j:outputPanel id="messagesField" ajaxRendered="true"> <h:messages id="messages" globalOnly="true" styleClass="message" errorClass="errormsg" infoClass="infomsg" warnClass="warnmsg" rendered="#{showGlobalMessages != 'false'}" /> </a4j:outputPanel> <ui:insert name="body" /> </rich:layoutPanel> </rich:layout>
I suppose that after the exception is thrown the reRender of the ajax request does not happen. I tried a lot and searched the www but did not find a hint that helped me.
Any suggestions?
Thanks