xmlvalidationxpagesxsp

How to validate a checkbox using validator in XPages


How do I validate a checkbox using a validator server side, the following code validates the inputbox but not the checkbox

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
    <xp:this.data>
        <xp:dominoDocument var="document1" formName="Order"></xp:dominoDocument>
    </xp:this.data>
    <xp:checkBox text="Nop" id="checkBox1" required="true" value="#{document1.Option1}" checkedValue="1">
        <xp:this.validators>
            <xp:validateRequired message="click checkbox"></xp:validateRequired>
        </xp:this.validators>
    </xp:checkBox>
    <xp:inputText id="inputText1" value="#{document1.Option2}">
        <xp:this.validators>
            <xp:validateRequired message="enter box"></xp:validateRequired>
        </xp:this.validators>
    </xp:inputText>
    <xp:button value="Label" id="button1">
        <xp:eventHandler event="onclick" submit="true" refreshMode="complete">
            <xp:this.action>
                <xp:saveDocument var="document1"></xp:saveDocument>
            </xp:this.action>
        </xp:eventHandler>
    </xp:button>
    <xp:messages id="messages1"></xp:messages>
</xp:view>

Solution

  • Use a custom validator instead of required parameter and required validator.

    <xp:checkBox text="Nop" id="checkBox1" value="#{document1.Option1}" checkedValue="1">
        <xp:this.validators>
            <xp:customValidator>
                <xp:this.validate><![CDATA[#{javascript:
                    if (value != "1") {
                        this.setValid(false);
                        return "click checkbox";
                    }
                    return null;
                }]]></xp:this.validate>
            </xp:customValidator>
        </xp:this.validators>
    </xp:checkBox>
    

    Check box gets validated on server this way.

    enter image description here