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

  • SEARCH
  • Home
  • 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 6978407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:45:07+00:00 2026-05-27T17:45:07+00:00

I’m coming with a question on JSF2. I’m currently mixing two different pieces of

  • 0

I’m coming with a question on JSF2. I’m currently mixing two different pieces of code proposed by BalusC on his blog :

http://balusc.blogspot.com/2007/12/set-focus-in-jsf.html

http://balusc.blogspot.com/2007/12/validator-for-multiple-fields.html

The first let highlight in red the fields that do have an error message.
The second let validation be performed on multiple fields.

I’m looking for a way to have a single error message (dont want the message to be rendered twice) in the FacesContext be linked to several client ids (because the message do concern several fields due to the multiple fields validator).

Is it possible with the language basics ?
If I can, I’d like to avoid a hand-made system (it should work with a managed bean with “request” scope, the validator placing the clientIds that do have an error into a List accessed by the PhaseListener).

Thanks in advance for your tips. Could not see anything close to addMessage() on FacesContext that could do the work, but maybe there is a way…

  • 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-27T17:45:08+00:00Added an answer on May 27, 2026 at 5:45 pm

    If the message is appearing twice, then it means that you’re either firing the same validator by both components or that the validator is fired once but implicitly adding the message to the other component.

    I understand that you want to mark the both components as invalid (so that they get highlighted) and that you want only one message. In that case, you need to make sure that the validator is fired once and that the other component is retrieving an empty/null message.

    You only need to change the validator to retrieve the whole component as attribute instead of its value (note: I’ve in the meanwhile edited the old article accordingly; it has another benefits) and you need to change the phase listener to remove empty/null messages.

    E.g. in view:

    <h:outputLabel for="password" value="Password" />
    <h:inputSecret id="password" value="#{bean.password}" required="true">
        <f:validator validatorId="passwordValidator" />
        <f:attribute name="confirm" value="#{confirm}" />
    </h:inputSecret>
    <h:message for="password" styleClass="error" />
    
    <h:outputLabel for="confirm" value="Confirm password" />
    <h:inputSecret id="confirm" binding="#{confirm}" required="true" />
    <h:message for="confirm" styleClass="error" />
    

    and in validate() method:

    String password = (String) value;
    UIInput confirmComponent = (UIInput) component.getAttributes().get("confirm");
    String confirm = confirmComponent.getSubmittedValue();
    
    if (password == null || password.isEmpty() || confirm == null || confirm.isEmpty()) {
        return; // Let required="true" do its job.
    }
    
    if (!password.equals(confirm)) {
        confirmComponent.setValid(false);
        context.addMessage(confirmComponent.getClientId(context), new FacesMessage(null));
        throw new ValidatorException(new FacesMessage("Passwords are not equal."));
    }
    

    and in the phase listener:

    Iterator<String> clientIdsWithMessages = facesContext.getClientIdsWithMessages();
    
    while (clientIdsWithMessages.hasNext()) {
        String clientIdWithMessages = clientIdsWithMessages.next();
    
        if (focus == null) {
            focus = clientIdWithMessages;
        }
    
        highlight.append(clientIdWithMessages);
    
        if (clientIdsWithMessages.hasNext()) {
            highlight.append(",");
        }
    
        Iterator<FacesMessage> messages = facesContext.getMessages(clientIdWithMessages);
    
        while (messages.hasNext()) {
            if (messages.next().getSummary() == null) {
                messages.remove(); // Remove empty messages.
            }
        }
    }
    

    Related:

    • JSF doesn't support cross-field validation, is there a workaround?
    • How validate two password fields by ajax?

    Unrelated to the concrete problem, there’s in JSF2 by the way an other way to highlight invalid fields. You can do it by the new implicit #{component} variable in EL:

    <h:inputText styleClass="#{component.valid ? '' : 'error'}" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

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.