Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5840281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:43:10+00:00 2026-05-22T11:43:10+00:00

I would like to perform validation in some of my input components such as

  • 0

I would like to perform validation in some of my input components such as <h:inputText> using some Java bean method. Should I use <f:validator> or <f:validateBean> for this? Where can I read more about it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:43:10+00:00Added an answer on May 22, 2026 at 11:43 am

    The standard way is to implement the Validator interface.

    @FacesValidator("fooValidator")
    public class FooValidator implements Validator<String> {
    
        @Override
        public void validate(FacesContext context, UIComponent component, String value) throws ValidatorException {
            if (value == null || value.isEmpty()) {
                // Let required="true" handle it.
            }
    
            // ...
    
            if (valueIsInvalid) {
                throw new ValidatorException(new FacesMessage("Value is invalid!"));
            }
        }
    }
    

    The component argument represents the component to which the validator is attached, which is usually an instance of UIInput. The value argument represents the value to be validated, which is usually the submitted and converted value of the associated UIInput component.

    The @FacesValidator will register it to JSF with validator ID myValidator so that you can reference it in validator attribute of any <h:inputXxx>/<h:selectXxx> component as follows:

    <h:inputText id="foo" value="#{bean.foo}" validator="fooValidator" />
    <h:message for="foo" />
    

    If you consider the value invalid, then just throw ValidatorException. This way the input field will be marked invalid (i.e. UIInput#isValid() will return false), and the bean property behind the value attribute of the input field (the #{bean.foo}) won’t be updated with the submitted/converted value, and the message of the ValidatorException will be displayed in the <h:message> associated with the input field.

    Do note that checking for null/blank value should be delegated to the required="true" attribute like so:

    <h:inputText id="foo" value="#{bean.foo}" validator="fooValidator"
        required="true" requiredMessage="Please fill out foo" />
    <h:message for="foo" />
    

    You can also use EL in validator attribute of any <h:inputXxx>/<h:selectXxx> component wherein you reference a managed bean method having exactly the same method signature (the same method arguments) as Validator#validate(). I.e. taking FacesContext, UIComponent and Object arguments in this order.

    <h:inputText id="foo" value="#{bean.foo}" validator="#{bean.validateFoo}" />
    <h:message for="foo" />
    
    public void validateFoo(FacesContext context, UIComponent component, String value) throws ValidatorException {
        if (value == null || value.isEmpty()) {
            // Let required="true" handle it.
        }
    
        // ...
    
        if (valueIsInvalid) {
            throw new ValidatorException(new FacesMessage("Value is invalid!"));
        }
    }
    

    This is only useful if the validator needs to access another property present in the same managed bean. If it doesn’t need to, then this approach is considered tight-coupling (poor practice thus), and you should split out the validator to its own class implementing the Validator interface.

    In case you wish to validate based on multiple input values and/or bean properties, much better is to pass them as <f:attribute> of the component. More detail can be found in the answer to JSF doesn't support cross-field validation, is there a workaround?

    You can also use <f:validator> taghandler, which would be the only way if you intend to attach multiple validators on the same component:

    <h:inputText id="foo" value="#{bean.foo}">
        <f:validator validatorId="fooValidator" />
    </h:inputText>
    <h:message for="foo" />
    

    This will execute the @FacesValidator("fooValidator") shown above.

    You can also use <f:validator binding> to reference a concrete validator instance somewhere in the EL scope, which can be specified and supplied the following way:

    <h:inputText id="foo" value="#{bean.foo}">
        <f:validator binding="#{fooValidator}" />
    </h:inputText>
    <h:message for="foo" />
    
    @Named("fooValidator")
    public class FooValidator implements Validator<String> {
    
        @Override
        public void validate(FacesContext context, UIComponent component, String value) throws ValidatorException {
            if (value == null || value.isEmpty()) {
                // Let required="true" handle it.
            }
    
            // ...
    
            if (valueIsInvalid) {
                throw new ValidatorException(new FacesMessage("Value is invalid!"));
            }
        }
    }
    

    Note that thus @Named is being used instead of @FacesValidator. The old @ManagedBean is also supported here instead of @Named. Historically, this was a trick in order to be able to use @EJB and @Inject in a validator. See also How to inject in @FacesValidator with @EJB, @PersistenceContext, @Inject, @Autowired

    Or this way, which in turn can easily be supplied as a lambda:

    <h:inputText id="foo" value="#{bean.foo}">
        <f:validator binding="#{bean.fooValidator}" />
    </h:inputText>
    <h:message for="foo" />
    
    public Validator<String> getFooValidator() {
        return (context, component, value) -> {
            if (value == null || value.isEmpty()) {
                // Let required="true" handle it.
            }
    
            // ...
    
            if (valueIsInvalid) {
                throw new ValidatorException(new FacesMessage("Value is invalid!"));
            }
        };
    }
    

    Also here applies the same problem of tight-coupling when this validator doesn’t need any other property from the same bean.

    To get a step further, you can use JSR303 bean validation. This validates fields based on annotations. So you can have just a

    @Foo
    private String foo;
    

    Without the need to explicitly register any validator in XHTML side. If you’re using JPA for persistence, by default this validator will also be executed during insert/update in DB. Since it’s going to be a whole story, here are just some links to get started:

    • Hibernate Validator – Getting started
    • JSF 2.0 tutorial – Finetuning validation
    • How to perform JSF validation in actionListener or action method?

    There’s also a <f:validateBean> tag, but this is only useful if you intend to disable the JSR303 bean validation. You then put the input components (or even the whole form) inside <f:validateBean disabled="true">.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to perform some logic in the table.modifiedField method
I would like to use client-side Javascript to perform a DNS lookup (hostname to
I would like to post-process my app.config file and perform some token replacements after
I need to perform Diffs between Java strings. I would like to be able
Would like to get a list of advantages and disadvantages of using Stored Procedures.
I would like to sort an array in ascending order using C/C++ . The
I would like to have a reference for the pros and cons of using
I would like to implement a post build event that performs the following actions
Would like to create a strong password in C++. Any suggestions? I assume it
I would like to test a string containing a path to a file for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.