Steve Zavocki
Steve Zavocki

Reputation: 1840

Cannot get Custom Validator to fire

I am stumped. I have an input field in an xpage where I added a custom validator. I can not get the custom validator to fire no matter what I try. There is a also validateRequired which works as expected. The error message is sent to a "Display Error" control, clientside validation is disabled. The goal of the custom validator is to check if a valid date has been entered. After I get it working, I also plan to add other complicated validation such as checking valid day of the week.

The print statements work correctly, as in "inside true" shows in the console for a valid date, and "inside false" shows for an invalid date. If I add a test print statement after the if statement, it doesn't not execute as you would think. For either condition, the message does not display, and the page is submitted.

I have tried using both getValue() and getSubmittedValue. I have also tried returning a facesMessage and a string. I am also very open to better ways to do this.

<xp:inputText id="datepicker" styleClass="datepicker" 
value="#{viewScope.arrivalDate}" 
defaultValue="#{javascript:statusBean.arrivalDate}"
disableClientSideValidation="true">
      <xp:this.validators>
        <xp:validateRequired message="Please enter the arrival date">
        </xp:validateRequired>
        <xp:customValidator message="err" loaded="true">
            <xp:this.validate><![CDATA[#{javascript:var datepicker = getComponent("datepicker").getSubmittedValue();
            var testDate = @IsTime(@TextToTime(datepicker));
            print("testDate=" + testDate);
            if(testDate == 0){
             print("inside false");
             return false;
        } else if(testDate == 1){
             print("inside true");
             return true;
            }}]]>
           </xp:this.validate>
        </xp:customValidator>
     </xp:this.validators>

Upvotes: 1

Views: 1113

Answers (3)

Adibabu Kancharla
Adibabu Kancharla

Reputation: 366

Here's my version with using xSnippet - postValidationError.

<xp:customValidator>
    <xp:this.validate><![CDATA[#{javascript:
        if (@IsTime(@TextToTime(@Trim(value))) == 0){
          postValidationError(this, "Please enter a valid Date/Time")
        } 
    }]]></xp:this.validate>
</xp:customValidator>

Upvotes: 1

Paul Stephen Withers
Paul Stephen Withers

Reputation: 15729

I haven't tried with a custom validator, but here's the SSJS I put in the validator property for a sample demo at LUGs last year:

<xp:this.validator>
<![CDATA[#{javascript:var sv = this.getSubmittedValue();
print ("Checking submitted value for input1 '" + sv + "' is more than 5 characters");
print ("Value for input1 is " + this.getValue());
if (sv.length < 5) {
    var msgStr = "You must enter your FULL name, which will be more than 5 characters!";
    var msgObj = new javax.faces.application.FacesMessage(javax.faces.application.FacesMessage.SEVERITY_ERROR, msgStr, msgStr);
    facesContext.addMessage(getClientId(this.getId()), msgObj);
    this.setValid(false);
}}]]>
</xp:this.validator>

Note that it adds the facesMessage and also sets the component's valid property to false.

Upvotes: 3

Per Henrik Lausten
Per Henrik Lausten

Reputation: 21709

The customValidator must return a string with the error message in it - and not true or false.

So in your case, do this instead:

<xp:customValidator message="err" loaded="true">
    <xp:this.validate><![CDATA[#{javascript:
        var datepicker = getComponent("datepicker").getSubmittedValue();
        var testDate = @IsTime(@TextToTime(datepicker));
        print("testDate=" + testDate);
        if(testDate == 0){
            print("inside false");
            return "Invalid date";
        }
    }]]></xp:this.validate>
</xp:customValidator>

Upvotes: 3

Related Questions