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 6539509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:52:02+00:00 2026-05-25T10:52:02+00:00

I’m building on BalusC’s solution to highlight and focus fields in JSF. My plan

  • 0

I’m building on BalusC’s solution to highlight and focus fields in JSF. My plan is to output a JSON array with ids and then have a method be called which will process this array. This works fine when I don’t use <f:ajax/>

Here is my phase listener solution:

public void beforePhase(PhaseEvent event) {
    FacesContext facesContext = event.getFacesContext();
    List<String> highlightFields = new ArrayList<String>();

    Iterator<String> highlightFieldsItr = facesContext
            .getClientIdsWithMessages();
    while (highlightFieldsItr.hasNext()) {
        StringBuilder sb = new StringBuilder();
        sb.append("#");
        sb.append(highlightFieldsItr.next().replaceAll(":", "\\\\:"));
        highlightFields.add(sb.toString());
    }
    JSONArray jsonHighlightFields = new JSONArray(highlightFields);

    facesContext.getExternalContext().getRequestMap()
            .put("errorFields", jsonHighlightFields.toString());
}

Basically this would produce errorFields value with something like [“#some\:id1”, “#some\id2”]. Then I can do something like this in my root layout file:

   <script>
     var errorFields = ${errorFields}; // This will xlate to ["#some\\:id1", "#some\\:id2"

     $(document).ready(function(){
         processInputErrors(errorFields);
     });
   </script>

With a processInputErrors function like this:

function processInputErrors(ids) {
    for (id in ids) {
        if (focus == false) {
            jQuery(ids[id]).focus();
            focus = true;
        }
        jQuery(ids[id]).addClass('input-error');
    }
}

However, I need to somehow obtain this list in the function which gets called on success of an ajax post.

Now f:ajax does have the onevent attribute and this function does get called, but I’m not sure exactly what it gets passed. How would I be able somehow pass the invalid Ids from the phase listener to this function? It seems to be passed an object which represents the HTMLInputElement?

  <f:ajax event="change" onevent="test" render="test test_msg" immediate="true" />

Happy to hear about alternative suggestions or ideas. The goal is basically to focus and highlight the field(s) which are invalid not only on a full post-back but also when using f:ajax.

Thanks!

  • 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-25T10:52:02+00:00Added an answer on May 25, 2026 at 10:52 am

    That article was more targeted on JSF 1.x. JSF 2.x offers now a lot of advantages of which the following are beneficial for your particular requirement:

    • You can refer the current component in EL by #{component}. In case of input components this is the UIInput which in turn has an isValid() method. This could be used in styleClass attribute.
    • You can use <f:ajax> to re-render parts of the view, also <script> elements.

    1+1 is…

    <h:inputText id="input1" value="#{bean.input1}" required="true" styleClass="#{component.valid ? '' : 'error'}">
        <f:ajax render="@this input1message focus" />
    </h:inputText> 
    <h:message id="input1message" for="input1" />
    ...
    <h:inputText id="input2" value="#{bean.input2}" required="true" styleClass="#{component.valid ? '' : 'error'}">
        <f:ajax render="@this input2message focus" />
    </h:inputText> 
    <h:message id="input2message" for="input2" />
    ...
    <h:panelGroup id="focus"><script>jQuery(":input.error:first").focus();</script></h:panelGroup>
    

    No need for a PhaseListener anymore. You could if necessary wrap the input markup boilerplate in a composite component or a tag file.


    Back to your concrete question about the onevent attribute, check this answer: JSF 2: How show different ajax status in same input?

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
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 have a text area in my form which accepts all possible characters from

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.