0

EDIT: I have this snippet of code:

<h:inputText id="email_id" value="#{CreateUserManager.email}" 
        styleClass="#{CreateUserManager.emailPrimariaValid ? '' : 'inputErrorClass'}">
    <f:validator validatorId="EmailValidator" />
    <a4j:support event="onblur" reRender="email_id, messages" oncomplete="setAnchor();"                         
        status="status4divCoverAll" ajaxSingle="true" />
</h:inputText>

This is the managed session bean:

public class CreateUserManager {
...
protected boolean emailPrimariaValid;

public CreateUserManager() {
    ...
    this.emailPrimariaValid = true;
}


public boolean isEmailPrimariaValid() {
    FacesContext context = FacesContext.getCurrentInstance();
    UIInput input = (UIInput)context.getViewRoot().findComponent(":createUser:email_id");
    return input.isValid();
}

public void setEmailPrimariaValid(boolean emailPrimariaValid) {
    this.emailPrimariaValid = emailPrimariaValid;
}

}

Keep in mind that I remove this bean from session if I come from another page (url), so the bean execute the constructor again.

The problem: I write an invalid email and it sets correctly the class to inputErrorClass, but if I go to another page (so the input component is still invalid) and then come back to the first one, the class remains to inputErrorClass.

argon argon
  • 159
  • 1
  • 1
  • 8
  • Is this really a "session bean", as in an EJB? Or do you mean "session-scoped managed bean"? Without being able to see the annotations, it's tough to tell. – Jeremiah Orr Jan 25 '12 at 18:45
  • It is a session scoped managed bean, the jsf version is 1.2 – argon argon Jan 25 '12 at 22:33
  • What debugging have you done? When you come back to the page, is isEmailPrimariaValid() returning true? – Jeremiah Orr Jan 26 '12 at 13:05
  • When I come back that method returns false, it seems somehow it stores the invalid state of the component although I remove the managed bean from the session! – argon argon Jan 26 '12 at 14:18

2 Answers2

0

Are you by any chance using Seam? It has some good functionality for styling input elements when there are errors.

In Seam 2, you can use the <s:decorate> tag: http://docs.jboss.org/seam/2.2.0.GA/reference/en-US/html/controls.html#d0e28688

In Seam 3, you can use UIInputContainer and a composite component: http://jerryorr.blogspot.com/2011/10/replacement-for-sdecorate-in-seam-3.html

If you aren't using Seam... well, you can look at the Seam source code to see what they did!

Jeremiah Orr
  • 2,620
  • 1
  • 18
  • 24
  • I don't use seam and I think it could be more difficult read their source code. I "just" need to re-initialize the component state which was modified by the validator. – argon argon Jan 25 '12 at 15:32
  • I don't think manually modifying the component's styleClass attribute is the way to go, then. See the link @dcernahoschi provided below for a JSF-only way to do it. – Jeremiah Orr Jan 25 '12 at 15:59
0

One of the many approaches :

http://mkblog.exadel.com/2011/05/how-to-hightlight-a-field-in-jsf-when-validation-fails/

dcernahoschi
  • 14,968
  • 5
  • 37
  • 59
  • I tried this approach, but the problem remains...when I change page and then return to the first one, the input component still has the error class. I think I have to initialize (in the constructor) the input.isValid to true, but I don't know how! The viewRoot is not accessible during that phase. – argon argon Jan 25 '12 at 17:29
  • I suggest posting some more code, like your SessionBean and the relevant section in your .xhtml (now that you've added the isValid check). – Jeremiah Orr Jan 25 '12 at 17:43