tags:

views:

1665

answers:

2

Hi All,

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?

+3  A: 

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.

McDowell
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.
Barun
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.
Drew
@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.
McDowell
@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
@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.
Drew
+1  A: 

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

Drew
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..
Barun
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.
Drew
Same state here...problems concerned with maintaining state...we are going with Spring Webflow...but thats for another topic..
Barun
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.
Drew