1

My entity has bean validation annotations @NotBlank and @Size on the name field. However, when I submit the JSF form with the name field left blank, it doesn't validate the field.

Here's the field in question:

@NotBlank(message = "{name.required}")
@Size(max = 25, message = "{long.value}")
@Column(name = "name", length = 25, nullable = false)
private String name;

When submitting the form without entering any data, I am getting the following exception:

Caused by: javax.faces.el.EvaluationException: javax.validation.ConstraintViolationException: validation failed for classes [com.myapp.domain.Department] during persist time for groups [javax.validation.groups.Default, ]
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    ... 89 more
Caused by: javax.validation.ConstraintViolationException: validation failed for classes [com.myapp.domain.Department] during persist time for groups [javax.validation.groups.Default, ]
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:132)
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:71)
    at org.hibernate.action.EntityInsertAction.preInsert(EntityInsertAction.java:177)
    at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:72)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:268)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:260)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:179)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
    at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at $Proxy212.addDepartment(Unknown Source)
    at com.myapp.beans.DepartmentBean.addOrUpdateDepartment(DepartmentBean.java:105)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:262)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)

How is this caused and now can I solve it?


Update: here's the relevant part of the view:

<h:form>         
    <ice:panelGrid columns="3">
        <h:outputLabel>Department Name:</h:outputLabel>
        <h:inputText id="name" value="#{departmentBean.departmentObj.name}" />
        <h:message for="name" />
    </ice:panelGrid>        
</h:form>
<h:form>
    <ice:panelGroup>
        <h:commandLink value="Add New" action="#{departmentBean.addOrUpdateDepartment}" />
    </ice:panelGroup>
</h:form>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498

1 Answers1

3

From the stacktrace:

at com.myapp.beans.DepartmentBean.addOrUpdateDepartment(DepartmentBean.java:105)
...
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)

This means that your JSF bean action method is invoked anyway while this should have been blocked by the validation constraints on the empty name field and you should have seen a validation error in a <h:message> or <h:messages> of the form.

That can only mean that you've put immeditate="true" on the command button/link which would cause all input components which don't have this attribute will be skipped in form processing:

<h:commandButton ... immediate="true" />

or that you've this context parameter in your web.xml which would skip validation of empty fields:

<context-param>
    <param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
    <param-value>false</param-value>
</context-param>

or that you have disabled the bean validation in your view by the following tag:

<f:validateBean disabled="true" />

Make sure that you have none of them.


Update: as per your update, you've made a classic mistake: you've put the submit button in a different form than the input fields. The submit button needs to go in the same form as the input fields which you would like to send along with the submit.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • strange thing that i don't have any of the two guesses, any other ideas ? – Mahmoud Saleh Nov 24 '11 at 18:12
  • Well, then apparently the process validations phase is completely skipped for the input components. Don't you have an `immediate="true"` on the command button? – BalusC Nov 24 '11 at 18:15
  • Your command button is in a different form. Surely it won't submit the inputs of another form. Put the inputs and the commands in the **same** form. Strangely enough this is exactly what you've [answered](http://stackoverflow.com/questions/8230451/how-to-add-iceselectmanycheckbox-to-a-icedatatable/8257067#8257067) to another question with similar problem today and also asked to me in a [comment](http://stackoverflow.com/questions/2524514/how-to-use-jsfs-hselectbooleancheckbox-with-hdatatable-to-create-one-object-p/2524832#comment10160524_2524832) for confirmation... – BalusC Nov 24 '11 at 19:08