I have several fields that I would like to validate before my bean is persisted.
I have inputText, selectOne, SelectManyListBox, ...
My problem is that for some of those the validation is done and for some others not...
For ex. I have
<tr>
<td><h:outputLabel value="#{msg.subscriptionFormFieldPhone}:" /></td>
<td><h:inputText
id="phone"
value="#{detailModel.afterObject.phone}"
disabled="#{detailModel.mode == detailModel.viewMode or (detailModel.mode == detailModel.editMode and !loggedUser.hasPermission('CMN backoffice permission id'))}" />
<h:messages for="phone" style="color:red; font-size:12px;" />
</td>
</tr>
...
<tr>
<td><h:outputLabel value="#{msg.subscriptionFormFieldFund}:" /></td>
<td>
<table>
<tr>
<td><h:inputText
id="fund"
value="#{detailModel.afterObject.fund}" disabled="true">
<f:converter converterId="ch.ethz.id.cmn.FundConverter" />
</h:inputText>
<h:messages for="fund" style="color:red; font-size:12px;" />
</td>
<td valign="top">
<!-- add button -->
<h:commandButton styleClass="waiButton"
value="#{lakshmiMsg.commonButtonAddExisting}"
action="#{detailController.gotoFundSelection()}"
rendered="#{(detailModel.mode == detailModel.createMode and detailModel.afterObject.fund == null) or ((detailModel.mode != detailModel.viewMode and detailModel.afterObject.fund == null))}"
disabled="#{detailModel.mode == detailModel.multiEditMode}" />
<!-- delete button -->
<h:commandButton styleClass="waiButton"
value="#{lakshmiMsg.commonButtonDelete}"
action="#{detailController.deleteFund()}"
rendered="#{((detailModel.mode == detailModel.editMode) and loggedUser.hasPermission('CMN backoffice permission id')) or (detailModel.mode == detailModel.createMode and detailModel.afterObject.fund != null)}"
disabled="#{detailModel.mode == detailModel.multiEditMode}" />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td><h:outputLabel value="#{msg.subscriptionFormFieldOwner}:" /></td>
<td>
<table>
<tr>
<td><h:inputText id="owner"
value="#{detailModel.afterObject.owner}"
disabled="true">
<f:converter converterId="ch.ethz.id.cmn.UserConverter"></f:converter>
</h:inputText>
<h:messages for="owner" style="color:red; font-size:12px;" />
</td>
<td valign="top">
<!-- add button -->
<h:commandButton styleClass="waiButton"
value="#{lakshmiMsg.commonButtonAddExisting}"
action="#{detailController.gotoUserSelection()}"
rendered="#{(detailModel.mode == detailModel.createMode and detailModel.afterObject.owner == null) or ((detailModel.mode != detailModel.viewMode and detailModel.afterObject.owner == null))}"
immediate="true"
disabled="#{detailModel.mode == detailModel.multiEditMode}" />
<!-- delete button -->
<h:commandButton styleClass="waiButton"
value="#{lakshmiMsg.commonButtonDelete}"
action="#{detailController.deleteOwner()}"
rendered="#{((detailModel.mode == detailModel.editMode) and loggedUser.hasPermission('CMN backoffice permission id')) or (detailModel.mode == detailModel.createMode and detailModel.afterObject.owner != null)}"
immediate="true"
disabled="#{detailModel.mode == detailModel.multiEditMode}" />
</td>
</tr>
</table>
</td>
</tr>
...
<tr>
<td valign="top">
<h:outputLabel value="#{msg.subscriptionFormFieldDataOptions}:" /></td>
<td>
<table>
<tr>
<td>
<h:selectManyListbox
id="dataOptions"
value="#{detailModel.selectedDataOptions}"
size="5"
disabled="#{detailModel.mode == detailModel.viewMode or (detailModel.mode == detailModel.editMode and !loggedUser.hasPermission('CMN backoffice permission id'))}">
<f:selectItems value="#{detailModel.afterObject.dataOptions}"
var="sdo" itemLabel="#{sdo.dataOptionName}"
itemValue="#{sdo}" />
</h:selectManyListbox>
<h:messages for="dataOptions" style="color:red; font-size:12px;" />
</td>
...
</tr>
</table>
</td>
</tr>
...
In my bean (which is bound with detailModel.afterObject) I tagged the fields with @NotNull and @Size(min=1).
@NotNull( message = "{validation.emptyPhoneNumber}" )
@Pattern( regexp = "^(((41)(\\s?)(-?))|(0))(7)(\\d{1})(\\s?)(-?)(\\d{3})(\\s?)(-?)(\\d{2})(\\s?)(-?)(\\d{2})", message = "{validation.notValidPhoneNumber}" )
private String phone;
@NotNull( message = "{validation.emptyFund}" )
@Size(min=1, message = "{validation.emptyFund}" )
private String fund;
@NotNull( message = "{validation.emptyOwner}" )
@Size(min=1, message = "{validation.emptyOwner}" )
private String owner;
@ManyToMany( fetch = FetchType.LAZY, cascade = {} )
@JoinTable( name = "CMN_MAP_SUBSCRIPTION_TO_DATAOPTION" )
@NotNull( message = "{validation.emptyDataOption}" )
@Size(min=1, message = "{validation.emptyDataOption}" )
private Set< DataOption > dataOptions;
Well, the first inputText (phone) is validated and if I leave it empty or the pattern doesn't match I get the error message, but for the others there is (apparently) no validation; if I leave them empty, no messages appear...
The only difference is that for the fund and owner inputFileds there is a converter and the last is a selectManyListBox which is mapped to a Set while all the others are mapped to String.
As the persistency then fails, I have an error message in the log and there I can see a ConstraintViolationException, but I don't understand why it does not claim at validation-time shwoing the messages on the page instead of a stack trace in the log...
[exec] [#|2013-09-17T11:26:49.901+0200|WARNING|glassfish3.1.2|javax.enterprise.resource.jta.com.sun.enterprise.transaction|_ThreadID=420;_ThreadName=Thread-2;|DTX5014: Caught exception in beforeCompletion() callback:
[exec] javax.persistence.PersistenceException: error during managed flush
[exec] at org.hibernate.ejb.AbstractEntityManagerImpl$CallbackExceptionMapperImpl.mapManagedFlushFailure(AbstractEntityManagerImpl.java:1515)
[exec] at org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorImpl.beforeCompletion(SynchronizationCallbackCoordinatorImpl.java:109)
[exec] at org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization.beforeCompletion(RegisteredSynchronization.java:53)
[exec] at com.sun.enterprise.transaction.JavaEETransactionImpl.commit(JavaEETransactionImpl.java:435)
[exec] at com.sun.enterprise.transaction.JavaEETransactionManagerSimplified.commit(JavaEETransactionManagerSimplified.java:855)
[exec] at com.sun.enterprise.transaction.UserTransactionImpl.commit(UserTransactionImpl.java:208)
[exec] at ch.ethz.id.wai.lakshmi.engine.common.TransactionHelper.commitTransaction(TransactionHelper.java:74)
[exec] at ch.ethz.id.wai.lakshmi.stdcmp.boo.processing.BusinessObjectOrderProcessing.processOrder(BusinessObjectOrderProcessing.java:121)
[exec] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[exec] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
[exec] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[exec] at java.lang.reflect.Method.invoke(Method.java:601)
[exec] at ch.ethz.id.wai.lakshmi.engine.ejb.LakshmiServerBean.invokeWorkflowStep(LakshmiServerBean.java:850)
[exec] at ch.ethz.id.wai.lakshmi.engine.ejb.LakshmiServerBean.executeNextWorkflowStep(LakshmiServerBean.java:796)
[exec] at ch.ethz.id.wai.lakshmi.engine.ejb.LakshmiServerBean.executeNextWorkflowStep(LakshmiServerBean.java:707)
[exec] at ch.ethz.id.wai.lakshmi.engine.ejb.LakshmiServerBean.continueWorkflow(LakshmiServerBean.java:627)
[exec] at ch.ethz.id.wai.lakshmi.engine.ejb.LakshmiServerBean.processLakshmiComponentResponse(LakshmiServerBean.java:576)
[exec] at ch.ethz.id.wai.lakshmi.engine.ejb.LakshmiServerBean.onMessage(LakshmiServerBean.java:397)
[exec] at sun.reflect.GeneratedMethodAccessor967.invoke(Unknown Source)
[exec] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[exec] at java.lang.reflect.Method.invoke(Method.java:601)
[exec] at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052)
[exec] at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124)
[exec] at com.sun.ejb.containers.BaseContainer.invokeTargetBeanMethod(BaseContainer.java:4180)
[exec] at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5368)
[exec] at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5348)
[exec] at com.sun.ejb.containers.MessageBeanContainer.deliverMessage(MessageBeanContainer.java:1099)
[exec] at com.sun.ejb.containers.MessageBeanListenerImpl.deliverMessage(MessageBeanListenerImpl.java:81)
[exec] at com.sun.enterprise.connectors.inbound.MessageEndpointInvocationHandler.invoke(MessageEndpointInvocationHandler.java:171)
[exec] at com.sun.proxy.$Proxy409.onMessage(Unknown Source)
[exec] at com.sun.messaging.jms.ra.OnMessageRunner.run(OnMessageRunner.java:260)
[exec] at com.sun.enterprise.connectors.work.OneWork.doWork(OneWork.java:114)
[exec] at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.performWork(ThreadPoolImpl.java:497)
[exec] at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:540)
[exec] Caused by: javax.validation.ConstraintViolationException: Validation failed for classes [ch.ethz.id.wai.cmn.bo.Subscription] during persist time for groups [javax.validation.groups.Default, ]
[exec] List of constraint violations:[
[exec] ConstraintViolationImpl{interpolatedMessage='Please specify a fund', propertyPath=fund, rootBeanClass=class ch.ethz.id.wai.cmn.bo.Subscription, messageTemplate='{validation.emptyFund}'}
[exec] ConstraintViolationImpl{interpolatedMessage='Please specify a fund', propertyPath=fund, rootBeanClass=class ch.ethz.id.wai.cmn.bo.Subscription, messageTemplate='{validation.emptyFund}'}
[exec] ConstraintViolationImpl{interpolatedMessage='Please specify a owner', propertyPath=owner, rootBeanClass=class ch.ethz.id.wai.cmn.bo.Subscription, messageTemplate='{validation.emptyOwner}'}
[exec] ConstraintViolationImpl{interpolatedMessage='{validation.emptyDataOption}', propertyPath=dataOptions, rootBeanClass=class ch.ethz.id.wai.cmn.bo.Subscription, messageTemplate='{validation.emptyDataOption}'}
[exec] ]
[exec] at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:159)
[exec] at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:94)
[exec] at org.hibernate.action.internal.EntityInsertAction.preInsert(EntityInsertAction.java:185)
[exec] at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:81)
[exec] at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
[exec] at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)
[exec] at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:275)
[exec] at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
[exec] at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
[exec] at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1213)
[exec] at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:402)
[exec] at org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorImpl.beforeCompletion(SynchronizationCallbackCoordinatorImpl.java:104)
[exec] ... 32 more
[exec] |#]
Help, hints, tips are welcome...
UPDATE:
I wrote and added a Validator to the phone inputText and fund inputText:
<f:validator validatorId="ch.ethz.id.cmn.validator.SubscriptionValidator" />
and I set a breakpoint in the validate method.
Well, for the phone the method is called, for the fund not!
I'm very confused. Something in my html code is preventing the validation...
UPDATE 2:
If I remove disabled="true"
, than it works. Is it so wanted or is a bug? Is there a way to have it work also if the field is disabled?
UPDATE 3:
Now I'm really confused... After having replaced disabled with readonly all (but the selectManyListBox) seamed to work well until I tried to edit some value in the page after having persisted it. Although I didn't change/delete the fund and owner fields, I get the validation error messages... I placed a breakpoint in the getters method of the bean and the values are set, i.e aren't null. So why the @NotNull still complain? This happens also if I remove the annotations in the bean and add the required and requiredMessage in the html.