Important update (2015-11-12)

A more recent implementation is available in all projets generated by Celerio.

Follow our doc to ** generate in a few minutes ** your first project with Celerio

How to skip JSF BeanValidation conditionally

Here is a trick that allows you to perform JSF-based BeanValidation depending on the action.

The challenge

The challenge when developing JSF applications involving multi-page form is to provide a usable navigation between the pages.

By usable navigation we mean:


The trick

We leverage the binding attribute of the JSF <f:validateBean/> tag.

As the documentation says, this attribute value is a ValueExpression that evaluates to an object that implements javax.faces.validate.BeanValidator

Unsurprisingly BeanValidator implements the validate method below from the Validator interface:

    public void validate(FacesContext context,
                         UIComponent component,
                         Object value) throws ValidatorException;

We just have to extend BeanValidator and override the validate method, here is the code:

package com.jaxio.demo.web.component;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.BeanValidator;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

 * Lenient bean validator which disable validation in certain cases 
 * in order to let the user navigate to sub view without loosing the 
 * data entered in input field.  
public class LenientBeanValidator extends BeanValidator {
    public void validate(FacesContext context, UIComponent component, Object value) {
        if (doValidation(context)) {
            super.validate(context, component, value);
        // simply skip it...

    private boolean doValidation(FacesContext context) {
        // main page (action that saves the main entity) 
        if (context.getExternalContext().getRequestParameterValuesMap().containsKey("form:save")) {
            return true;

        // sub page (action that binds the entered data to an associated entity)
        if (context.getExternalContext().getRequestParameterValuesMap().containsKey("form:ok")) {
            return true;

        return false;

Of course the code above may be improved to fit your need and your security requirements.

How to use it

Then, to use it from your view, you simply do as follow:

  <h:form id="form">
  	<f:validateBean disabled="true"> 
    	<!-- ... skip for clarity ... -->
		<p:inputText id="email" label="${msg.account_email}" value="#{}" maxlength="80" >
    		<f:validateBean binding="#{lenientBeanValidator}"/>
        <!-- 'save' (validation is enforced) -->
		<p:commandButton id="save" action="save" ajax="true" update="messages" process="@form"
			styleClass="aria-save-button default"
			rendered="#{not subPage}" />

        <!-- 'ok' (validation is enforced) -->
   		<p:commandButton id="ok" action="ok" process="@form" ajax="true" update="messages" 
   			rendered="#{subPage}" />    

        <!-- 'otherCase' (validation is NOT enforced) -->
   		<p:commandButton id="navigateInSubPage" action="navigateInSubPage" process="@form" ajax="false" 

If you have any remark on this solution, please share :)


The SpringFuse/Jaxio team.

comments powered by Disqus