6 Replies Latest reply on Apr 7, 2009 6:46 AM by Nick Belaevski

    a4j and jsf locale change problem

    Janusz Gren Newbie

      Hello,

      I'm very new to both jsf and a4j programming and I'm currently stuck and out of ideas on how to solve the problem mentioned in the topic.

      I'm using a4j to dynamically validate some inputfields on a form in following manner:

      <h:inputText id="firstNameInput" value="#{userBean.firstName}" size="40" maxlength="50" required="true">
      <f:validateLength minimum="5" maximum="50"/>
      <f:validator validatorId="nameValidator"/>
      <a4j:support event="onblur"
      immediate="true"
      actionListener="#{userBean.validateName}"
      reRender="errorMessage"/>
      </h:inputText>
      <h:outputText id="errorMessage" binding="#{userBean.error}"
      value="#{userBean.errorMessage}"
      style="color: red; font-style: italic;"/>


      and I also allow locale change with the following buttons:
      <h:commandLink action="#{changeLocaleBean.englishAction}" immediate="true">
      <h:graphicImage value="/images/english_flag.gif" style="border: 0px"/>
      </h:commandLink>

      where

      public String germanAction() {
      FacesContext context = FacesContext.getCurrentInstance();
      context.getViewRoot().setLocale(Locale.GERMAN);
      return null;
      }

      The locale change works perfectly well on all non-ajax components of the website however since the ajax component - errorMessage, obtains its value from the userBean and not directly from the message_*.properties files and the command button does not invoke a new validation the message stays in the previous locale.

      Is there any way to invoke verification, which would in due course rerender the proper errorMessage stratight after the locale change?

      Or is there perhaps some different way to manage the locale change or errorMessage display which would simplyfy or solve this problem?

      Thank you in advance for your reply.
      Janusz

        • 1. Re: a4j and jsf locale change problem
          Nick Belaevski Master

          Hello,

          You can make h:inputText immediate="true". This will cause validation to trigger for immediate action components also.

          • 2. Re: a4j and jsf locale change problem
            Janusz Gren Newbie

            Hello,

            Thanks for the idea - it does not however solve the problem.

            The thing is that the ajax component which is triggered by an onblur event retrieves the error message from a .properties file and it does not automatically react to locale change. The message gets properly adjusted to the correct locale after I trigger the onblur event myself manually.

            Maybe there is some way to trigger this event from the commandLinks straight after the locale change?

            Thanks
            Janusz

            • 3. Re: a4j and jsf locale change problem
              Nick Belaevski Master

              Janusz,

              Please post bean code. And what is bean scope?

              • 4. Re: a4j and jsf locale change problem
                Janusz Gren Newbie

                the userBean ajax validation code :

                private String errorMessage = null;

                public String getErrorMessage() {
                return errorMessage;
                }

                public void setErrorMessage(String errorMessage) {
                this.errorMessage = errorMessage;
                }

                public void validateName(ActionEvent e) {

                UIInput input = (UIInput)e.getComponent() // Ajax4jsf comp
                .getParent(); // input comp
                if (input != null) {
                String name = (String)input.getSubmittedValue();
                if (name != null) {
                //do something

                FacesContext fc = FacesContext.getCurrentInstance();
                input.validate(fc); // iterates over input's validators
                if ( ! input.isValid())
                setErrorMessage(fc, input);
                }
                }
                }
                private void setErrorMessage(FacesContext fc, UIInput input) {

                Iterator it = fc.getMessages(input.getClientId(fc));
                if (it.hasNext()) {
                FacesMessage facesMessage = (FacesMessage)it.next();
                errorMessage = facesMessage.getSummary();
                }
                }


                This code is triggered by an onblur event on the firstNameInput field and the errorMessage is retrieved from the errorMessage property.

                Now when the commandLink changes the locale the JSF changes all of the labels which directly access their strings via eg. #{msgs.firstName}, however since the errorMessage is taken from the bean property - it does not get changed automatically

                Janusz

                • 5. Re: a4j and jsf locale change problem
                  Janusz Gren Newbie

                  forgot to add that both the userBean and changelocaleBean are session scoped.

                  • 6. Re: a4j and jsf locale change problem
                    Nick Belaevski Master

                    I think the best you can do are two things: either a) call validating actions programmatically on locale switch
                    or b) use more common way of validation