1

Doing server-side Xpages conditional validation....

I want the follow validation to kick in only if the Report.Status field = 'Final'. (The users are allowed to submit the report as draft and come back and finish it later, when they submit as draft we don't want to make the fields required.)

How would I go about this?

    <xp:inputTextarea style="width:75%" value="#{Report.Agenda}" id="Agenda">
                            <xp:this.validators>
                    <xp:validateRequired
                        message="Question 1 can't be blank">
                    </xp:validateRequired><!-- (1)  -->
                </xp:this.validators>
    </xp:inputTextarea>

Tried this, didn't work, was still required even if Status field not set to final :{

    <xp:inputTextarea style="width:75%" value="#{Report.Agenda}" id="Agenda" defaultValue="5 year agenda">
                            <xp:this.validators>
                    <xp:validateRequired message="Question 1 can't be blank"></xp:validateRequired><!-- (1)  -->
                    <xp:validateExpression message="Question 1 can't be blank">
        <xp:this.expression><![CDATA[#{javascript:
            if (Report.getItemValueString('Status')!='Final') {
                return true;
            }
        else {
        return false;
        }
        }]]></xp:this.expression>
    </xp:validateExpression>
                </xp:this.validators>
    </xp:inputTextarea>

1 Answers1

4

Disable validation for whole XPage in submit button with property disableValidators if field "Status" is not "Final":

var status = getComponent('comboBoxStatus').getSubmittedValue();
if (!status || status !== 'Final') {return true;} return false;

The first code line determines the current value of Status control (e.g. combo box) at validation phase (otherwise it is null). The second line returns a false (= don't disable validators) if Status control is "Final" and otherwise true (= ignore all validators).

This way all validators in XPage get executed only if Status is "Final".

This is a working example:

<xp:comboBox id="comboBoxStatus" value="#{Report.Status}">
    <xp:selectItem itemLabel="Start"></xp:selectItem>
    <xp:selectItem itemLabel="Final"></xp:selectItem>
</xp:comboBox>
<xp:br />
<xp:inputTextarea
    id="inputTextarea1"
    value="#{Report.Agenda}"
    disableClientSideValidation="true">
    <xp:this.validators>
        <xp:validateRequired message="Question 1 can't be blank" />
    </xp:this.validators>
</xp:inputTextarea>
<xp:br />
<xp:messages
    id="messages1"></xp:messages>
<xp:br />
<xp:button
    value="Save"
    id="button1">
    <xp:eventHandler
        event="onclick"
        submit="true"
        refreshMode="complete"
        immediate="false"
        save="true"
        disableValidators="#{javascript:
            var status = getComponent('comboBoxStatus').getSubmittedValue();
            if (!status || status !== 'Final') {return true;} return false;}">
    </xp:eventHandler>
</xp:button>

Please notice that client side validation is disabled with disableClientSideValidation="true"

In case you want to disable validation only for certain elements in XPage then set property "disableValidators" only at those elements.

Knut Herrmann
  • 30,880
  • 4
  • 31
  • 67