jsfxpagescustomvalidator

XPages - validator not throwing a message to the screen


I have set up a validator class for an xp:input control:

<xp:inputText
    id="inpTeamName"
    title="${strings.label_name_team_title}"
    value="#{teamBean.team.name}"
    defaultValue="#{teamBean.team.name}"
    required="true"
    validator="#{teamValidator.valName}">
    <xp:this.attrs>
        <xp:attr
            name="placeholder"
            value="#{strings.label_name_team_ph}">
        </xp:attr>
    </xp:this.attrs>
    <xp:this.validators>
        <xp:validateRequired loaded="true"
            message="#{strings.teamValidateNameEmpty}">
        </xp:validateRequired>
        <xp:validateLength
            minimum="6"
            message="#{strings.teamValidateNameTooShort}">
        </xp:validateLength>
    </xp:this.validators>
</xp:inputText>
<xp:panel>
    <small
        id="infoHelp"
        class="form-text text-muted">
        <xp:text value="${strings.label_name_team_helper}" />
    </small>
</xp:panel>

And here is the method in the TeamValidator class (accessed as managed bean):

public void valName(FacesContext facesContext, UIComponent component, Object value) {          
    String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
    utils.printToConsole(this.getClass().getSimpleName().toString() + " " + methodName);
    try {           

        if (value.toString().replaceAll("\\s+","").equals("")){         
            String msg = propStrings.getProperty("teamValidateNameEmpty");
            FacesMessage message = new FacesMessage(msg);
            throw new ValidatorException(message);      
        } 

        //check for duplicate name for team for event
        String qParam = utils.getUrlParameterValue("unid");
        TeamBean teamBean = new TeamBean();
        ArrayList<JsonJavaObject> teams = teamBean.loadObjects();           
        boolean duplicateFound = false;
        for (JsonJavaObject team : teams) {
            String key = "team";
            if (team.containsKey(key)) {
                String name = team.getAsString(key).replaceAll("\\s+","");                  
                if (value.toString().replaceAll("\\s+","").equals(name)){
                    duplicateFound = true;
                }
            }
        }

        if (true == duplicateFound ) {              
            String msg = propStrings.getProperty("teamValidateNameDuplicate");
            FacesMessage message = new FacesMessage(msg);
            throw new ValidatorException(message);      
        }

    } catch (Exception e) {
        XspOpenLogUtil.logErrorEx(e, JSFUtils.getXSPContext().getUrl().toString(), Level.SEVERE, null);
    }
}

If I add some print statements I see that it detects duplicates and it should throw a validator exception. But that message does not appear on the screen (I added a xp:messages control).

The validation for required and length do appear on the screen though.

I also see that the code that triggers the validator (saving a document) is not prohibited by the validation.

I wonder what I am doing wrong here?

FYI the inputput control resides in a xe:dialog contorl.


Solution

  • Your thrown ValidatorException is caught by the try/catch in the same method. So remove the try/catch.