1

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>
Jonas
  • 121,568
  • 97
  • 310
  • 388
m benway
  • 245
  • 2
  • 10
  • I think the group name is only used for grouping in regards to displaying the radio boxes together? Is there any reason you don't want to use the standard radioGroup with validateRequired? – Chris Richards Nov 30 '16 at 13:47
  • The reason is because I have radio buttons to the same group laid out in very specific way: They are embedded in various cells in a table. My code above is just showing two of those cells. – m benway Nov 30 '16 at 15:59

2 Answers2

0

Fair enough... In that case, outside of doing something with CSJS, I'm not too sure....

Not sure if this is going to help, but have a look at this post by Marky Roden: https://xomino.com/2012/03/10/checking-xpages-radio-buttons-have-been-selected-with-jquery/

I haven't been doing any development work for 4/5 weeks as year end comes up so my brain isnt really working like it should.....

Chris Richards
  • 695
  • 5
  • 14
  • that solution suggests to use this to get the groupName of my radio buttons, but then where can i put this in order to do the validation? #{javascript:getComponent(“radio1”).getClientGroupName( facesContext, getComponent(“radio1”))} – m benway Dec 01 '16 at 21:14
  • Could you not do something like, if ($("[name$=CHECKBOXGROUPNAME]:checked").val() = '') { msg="Select the option\n"; } – Chris Richards Dec 02 '16 at 10:36
0

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" />
Knut Herrmann
  • 30,880
  • 4
  • 31
  • 67
  • It works. as it displays the error messages below my button. However, the "return false;" is not stopping the script from running there. It continues onto the document1.save(). It so happens that I increment a page counter on a multi-page survey, that displays a new set of questions on the same xpage. What do you think is the problem? – m benway Dec 02 '16 at 17:39
  • Return is return. It can't get to `document1.save()` line. Maybe you have `document1.save()` in a different action code or you use a "Submit" button with property `save="true"`... – Knut Herrmann Dec 02 '16 at 17:48
  • The button type is "Button". I dont have save="true", but I'm doing a Full Update . That shouldn't save the doc, though, until it gets to the document1.save() line, right? – m benway Dec 02 '16 at 18:40
  • Yes, it shouldn't save and it doesn't in my example. I use your first code block plus the code in my answer plus the data source definition: ` `. It saves the document only if one radio is selected. Could you create this easy example too and go from there? – Knut Herrmann Dec 02 '16 at 19:15
  • OK. it was a matter of the order in which I was updating some fields going page to page. Had to put all the events into a script insead of using simple actions, but that is fine. One more question: is there a way to write the error message to an Error Message control rather than the Error Messages? – m benway Dec 05 '16 at 19:33
  • You can set the target message client id as first parameter in facesContext.addMessage(). – Knut Herrmann Dec 05 '16 at 19:48
  • Knut, Appreciate this thread; hopefully someone else find it useful. I have been trying to use that first parameter, but it is not working.. I created an Error Message name "myError and changed the parameter: facesContext.addMessage("myError", new javax.faces.application.FacesMessage("select a radio")); – m benway Dec 05 '16 at 20:35