Search code examples
jsfxpages

Possible to set the styleClass of a xp:control from validator class?


For a form I have set up a validaor for an Edit Box e.g.:

<xp:inputText
    id="inpRiskFactors" 
    styleClass="inputEscalation"
    value="#{matterBean.matter.escRiskFactors}" 
    validator="#{matterValidators.valEscRiskFactors}"...

Here is a snippet from my validating method:

public void valEscRiskFactors(FacesContext facesContext, UIComponent component, Object value) {        
        utils.printToConsole(this.getClass().getSimpleName().toString() + " - valEscRiskFactors(...), value = " + value.toString());
        String msg = null;
        if (value.toString().replaceAll("\\s+","").equals("")){
            msg = matterProp.getProperty("msg_valid_esc_risk_factors");
            FacesMessage message = new FacesMessage(msg);
            throw new ValidatorException(message);
        }       
    }

I was wondering if I could change/set the styleclass property of the calling component (UIComponent component) from this method?

For the user it would be nice to apply some CSS on the xp:controls that are required and do not pass the validation.

Anyone have a clue how to achieve this?


Solution

  • The explicit call to setStyleClass method of UIComponent casted to XspInputText works for me

    ((XspInputText)component).setStyleClass("my-class");
    

    If you don't want to cast but want to apply the styleClass to any component that supports such property, java reflection can help:

    try {
        Method setStyleClass = component.getClass().getMethod("setStyleClass", String.class);
        if (setStyleClass != null) {
            setStyleClass.invoke(component, "my-class");
        }
    } catch (SecurityException e) {} 
    catch (NoSuchMethodException e) {}
    catch (IllegalArgumentException e) {}
    catch (IllegalAccessException e) {}
    catch (InvocationTargetException e) {}
    

    You may want to append the styleClass instead of replace

    try {
        String styleClass = null;
        Method getStyleClass = component.getClass().getMethod("getStyleClass");
        if (getStyleClass != null) {
            styleClass = (String)getStyleClass.invoke(component, (Object[])null);
        }
        Method setStyleClass = component.getClass().getMethod("setStyleClass", String.class);
        if (setStyleClass != null) {
            String newStyleClass = StringUtil.isNotEmpty(styleClass) ? styleClass.concat(" my-class") : "my-class";
            setStyleClass.invoke(component, newStyleClass);
        }
    } catch (SecurityException e) {} 
    catch (NoSuchMethodException e) {}
    catch (IllegalArgumentException e) {}
    catch (IllegalAccessException e) {}
    catch (InvocationTargetException e) {}