• cdi
  • components
  • contexts
  • converters
  • eventlisteners
  • exceptionhandlers
  • facesviews
  • filters
  • functions
  • managedbeans
  • renderkits
  • resourcehandlers
  • scripts
  • taghandlers
  • utils
  • validators
  • viewhandlers
-
  • Eager
  • FacesConverter
  • FacesValidator
  • Param
  • ViewScoped

The @FacesValidator is by default not eligible for dependency injection by @Inject nor @EJB. There is a workaround for EJB, but this is nasty and doesn't work out for CDI. Another way would be to make it a JSF or CDI managed bean, however this doesn't register the validator instance into the JSF application context, and hence you won't be able to make use of Application.createValidator(String) on it.

Initially, this should be solved in JSF 2.2 which comes with new support for dependency injection in among others all javax.faces.*.*Factory, NavigationHandler, ResourceHandler, ActionListener, PhaseListener and SystemEventListener instances. The Converter and Validator were initially also among them, but they broke a TCK test and were at the last moment removed from dependency injection support.

The support is expected to come back in JSF 2.3, but we just can't wait any longer. MyFaces CODI has support for it, but it requires an additional @Advanced annotation. OmniFaces solves this by implicitly making all FacesValidator instances eligible for dependency injection without any further modification, like as JSF 2.2 would initially do, hereby providing a transparent bridge of the code to the upcoming JSF 2.3.

The ValidatorManager provides access to all FacesValidator annotated Validator instances which are made eligible for CDI.

CDI issues in EAR

Note that CDI has known issues when OmniFaces is bundled in multiple WARs in a single EAR. It's important to understand that those issues are not related to OmniFaces, but to the broken CDI spec. For an overview of those issues, please refer Known issues of OmniFaces CDI features in combination with specific application servers.

Demo

Submit the form

Validator will print itself and both the injected EJB and CDI bean in a faces message. Note: EJB is stateless and CDI bean is request scoped.

Source code
<h3>Submit the form</h3>
<p>
    Validator will print itself and both the injected EJB and CDI bean in a faces message.
    Note: EJB is stateless and CDI bean is request scoped.
</p>
<h:form>
    <h:inputText validator="someValidator" />
    <h:commandButton value="Submit">
        <f:ajax execute="@form" render="@form" />
    </h:commandButton>
    <h:messages />
</h:form>