4

I am working on a requirement to highlight fields that have failed in red after JSF server side validation. No javascript can be used for validation. Is there a method to link server side validation with css style changes?

2 Answers 2

6

You could do this with a managed bean:

public class ValidBean {

  private UIComponent myComponent;

  public UIComponent getMyComponent() {
    return myComponent;
  }

  public void setMyComponent(UIComponent myComponent) {
    this.myComponent = myComponent;
  }

  public String getErrorStyle() {
    FacesContext context = FacesContext
        .getCurrentInstance();
    String clientId = myComponent.getClientId(context);
    Iterator<FacesMessage> messages = context
        .getMessages(clientId);
    while (messages.hasNext()) {
      if (messages.next().getSeverity().compareTo(
          FacesMessage.SEVERITY_ERROR) >= 0) {
        return "background-color: red";
      }
    }
    return null;
  }
}

Request scope variable:

  <managed-bean>
    <managed-bean-name>validBean</managed-bean-name>
    <managed-bean-class>stylevalid.ValidBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
  </managed-bean>

Sample view:

  <f:view>
    <h:form>
      <h:inputText binding="#{validBean.myComponent}" styleClass="foo"
        style="#{validBean.errorStyle}">
        <f:validateLength minimum="6" />
      </h:inputText>
      <h:commandButton />
      <h:messages />
    </h:form>
  </f:view>

The component is bound to the backing bean. If error messages have been queued for the component, it overrides its CSS class settings with its style attribute.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi this method looks good and works also but is injecting css styles from beans into jsp a standard practice is JSF. I mean from maintenance perspective its will entirely obvious that the change has to be made in Java classes. And if I want to add another style element it wont be possible. Its not a factor for me right now but I was just worried about any future problems.
Probably a slightly better alternative would be to dynamically determine the styleClass attribute. That way you would only need to change the CSS file rather than the Java class.
@Barun - you are correct; it is not an ideal solution. The core components tend to be a bit basic. Folk look to the control libraries for a richer experience. An alternative would be to make errorStyle a boolean and resolve the style value via a function: #{myfn:resolve(validBean.errorStyle, 'szErrStyle', 'szOkStyle')}. That way, you get fewer presentation layer details leaking into the back end.
@Drew - I'm not sure putting the CSS class names into the beans is any better than explicit styling. It could certainly be done, though.
@McDowell I would think it would be a little bit better because it still keeps all the styling rules in the CSS file. I can change all the red text to blue text by editing just the CSS file. On the other hand, I'd have to edit a collection of java files if I had a fair number of things like this I had to do. Neither are really ideal though because you're still tying the markup to the code pretty significantly.
1

The seam framework makes this very easy. Check this out: http://www.redhat.com/docs/manuals/jboss/jboss-eap-4.2/doc/seam/Seam_Reference_Guide/JSF_form_validation_in_Seam.html

4 Comments

Hi this method looks good but we are not using seam in the current project...a pity though because as Seam constructs really help out in many cases..
Sorry to hear that. Seam is really a killer app for JSF. I can sympathize, however, as I'm working on a JSF app now which doesn't use Seam and I'm finding maintaining state through pageflows annoying.
Same state here...problems concerned with maintaining state...we are going with Spring Webflow...but thats for another topic..
Yeah. I unfortunately have a home-rolled system that isn't too sweet but it works until we can get the code base to a point where we can begin refactoring.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.