James James - 4 months ago 17
Java Question

f:validateRequired does not work on Tomcat

I was using GlassFish on my first JSF project (still learning JSF) and everything was fine. But later, I switched to Tomcat (GlassFish was causing some problems and Tomcat hosting is more cheaply provided). And now I have a problem I just can't solve: validation of forms using f:validate stopped working.

I have already tried many solutions, the most mentioned solution is adding validation-api.jar and hibernate-validator.jar to libs - that just caused that the form stoped working at all (see error above).

Now I really don't know what to do. I am using NetBeans 7.1, Tomcat 7.0.22.0. Any help is appreciated. Thanks!

Oh yes...and interesting thing is that validating by regular expression (e-mail) works, but only if the input is not empty). (And without the libs added)

Error message after adding validation-api.jar and hibernate-validator.jar to libs and submiting the form:

javax.validation.ValidationException: Unable to find a default provider
at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:264)
at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:111)
at javax.faces.validator.BeanValidator.validate(BeanValidator.java:276)
at javax.faces.component.UIInput.validateValue(UIInput.java:1149)
at javax.faces.component.UIInput.validate(UIInput.java:967)
at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
at javax.faces.component.UIInput.processValidators(UIInput.java:698)
at javax.faces.component.UIForm.processValidators(UIForm.java:253)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172)
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:409)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:304)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)


The form:

<h:form>
<table>
<tr>
<th><label for="registration-email">#{usersTexts.email}:</label></th>
<td>
<h:inputText value="#{userRegistry.newUser.email}" id="registration-email" validatorMessage="#{msg.invalidEmail}" maxlength="100">
<f:validateRequired />
<f:validateRegex pattern="^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$" />
</h:inputText>
</td>
<td><p:message for="registration-email" /></td>
<td rowspan="4">
<!--p:captcha immediate="true" validatorMessage="#{msg.wrongCaptcha}" requiredMessage="#{formsTexts.captcha} #{msg.isEmpty}" /-->
</td>
</tr>
<tr>
<th><label for="registration-name">#{usersTexts.name}:</label></th>
<td>
<h:inputText value="#{userRegistry.newUser.name}" id="registration-name" validatorMessage="#{formsTexts.field} #{usersTexts.name} #{msg.isEmpty}" maxlength="100">
<f:validateRequired />
</h:inputText>
</td>
<td><p:message for="registration-name" /></td>
</tr>
<tr>
<th><label for="registration-password1">#{usersTexts.password}:</label></th>
<td>
<h:inputSecret value="#{userRegistry.newUser.password}" binding="#{password}" id="registration-password1" validatorMessage="#{formsTexts.field} #{usersTexts.password} #{msg.isEmpty}">
<f:validateRequired />
</h:inputSecret>
</td>
<td><p:message for="registration-password1" /></td>
</tr>
<tr>
<th><label for="registration-password2">#{usersTexts.passwordAgain}:</label></th>
<td>
<h:inputSecret id="registration-password2" validatorMessage="#{msg.passwordsNotEqual}">
<f:validateLength minimum="#{user.MIN_PASSWORD_LENGTH}" />
<f:validator validatorId="equality" />
<f:attribute name="matchAgainst" value="#{password}" />
</h:inputSecret>
</td>
<td><p:message for="registration-password2" /></td>
</tr>
<tr>
<td colspan="2" class="submit_row">
<h:commandButton value="#{usersTexts.register}" action="#{userRegistry.register}" />
</td>
</tr>
</table>
</h:form>

Answer

Well, with help of a friend I solved it...don't know why, but if input is empty, all f:validate tags are ignored - including f:validateRequired, so there was no validaton at all.

Solved that by removing f:validateRequired tags and using required="true" attribute. So, the input value cant be empty (required="true" works) and than, all other f:validators work.

Comments