Search code examples
validationradio-buttonxpagesgroupname

Xpages radio button validation by groupName?


I have radio buttons in a table with the same groupName. Is there a relatively simple way to validate this field upon submitting the form without using CCJS?

<xp:td>
    <xp:radio id="radio120" groupName="R_1" value="#{document1.R_1}" selectedValue="1"></xp:radio>
</xp:td>
<xp:td>
    <xp:radio id="radio120" groupName="R_1" value="#{document1.R_1}" selectedValue="2"></xp:radio>
</xp:td>

With regular Radio Button Group controls, I use validateRequired with an errorMessage control to display a message.

<xp:radioGroup styleClass="A2" style="border:0px;" value="#{document1.Necktie}" layout="pageDirection" id="Necktie">
    <xp:this.validators>
        <xp:validateRequired message="REQUIRED!"></xp:validateRequired>
    </xp:this.validators>
    <xp:selectItem itemLabel="No" id="selectItem13"></xp:selectItem>
    <xp:selectItem itemLabel="Yes" id="selectItem14"></xp:selectItem>
</xp:radioGroup>
<xp:message id="message10" for="Necktie"></xp:message>

Solution

  • Use the data source document1 on server side to find out if one of your radio buttons was selected.
    Test for document1.getItemValueString("R_1"). If it is empty set an error message and return false. Otherwise save the document.

    <xp:button value="Save" id="button1">
        <xp:eventHandler event="onclick" submit="true"
            refreshMode="complete">
            <xp:this.action><![CDATA[#{javascript:
                if (document1.getItemValueString("R_1") == "") {
                    facesContext.addMessage(null, 
                        new javax.faces.application.FacesMessage("select a radio"));
                    return false;
                }
                document1.save();
            }]]></xp:this.action>
        </xp:eventHandler>
    </xp:button>
    <xp:messages id="messages1" />