简体   繁体   中英

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>

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.

在此处输入图片说明

just a workaround and there may be better solutions. But if nothing else helps you could try to work with a second field or maybe a scope var:

idea #1: instead of using a validator have your button code query the checkbox's submittedValue. If that is foien for you continue else quit.

idea #2: don't bind the checkbox to a Notes data field but instead use it's onclick or onchange events to write the correct value to an EditBox which in turn is validated

again: there may be much better ways those two just popped up, and I haven't tried them. - Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM