maneesh7890
maneesh7890

Reputation: 23

Conditions based on the dynamic values of iterated controls in repeat section in Orbeon

I have a checkbox in the repeated section. It can only be ticked once in the iteration. Once it is ticked in the repeated section the rest must be read-only. ex: "Once Make him primary applicant" is checked then that checkbox in the other (previous as well as next) iterations must be disabled or hidden or read-only with null value. Please help me how to achieve this.

<xh:html xmlns:xh="http://www.w3.org/1999/xhtml" xmlns:xf="http://www.w3.org/2002/xforms"
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:ev="http://www.w3.org/2001/xml-events"
     xmlns:xi="http://www.w3.org/2001/XInclude"
     xmlns:xxi="http://orbeon.org/oxf/xml/xinclude"
     xmlns:xxf="http://orbeon.org/oxf/xml/xforms"
     xmlns:exf="http://www.exforms.org/exf/1-0"
     xmlns:fr="http://orbeon.org/oxf/xml/form-runner"
     xmlns:saxon="http://saxon.sf.net/"
     xmlns:sql="http://orbeon.org/oxf/xml/sql"
     xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
     xmlns:fb="http://orbeon.org/oxf/xml/form-builder">
<xh:head>
    <xh:title>Untitled Form</xh:title>
    <xf:model id="fr-form-model" xxf:expose-xpath-types="true">

        <!-- Main instance -->
        <xf:instance id="fr-form-instance" xxf:exclude-result-prefixes="#all" xxf:index="id">
            <form>

                <section-1>
                    <section-1-iteration>
                        <surname/>
                        <checkbox>false</checkbox>
                    </section-1-iteration>
                </section-1>
            </form>
        </xf:instance>

        <!-- Bindings -->
        <xf:bind id="fr-form-binds" ref="instance('fr-form-instance')">

            <xf:bind id="section-1-bind" ref="section-1" name="section-1">
                <xf:bind id="section-1-iteration-bind" ref="section-1-iteration"
                         name="section-1-iteration">
                    <xf:bind id="surname-bind" ref="surname" name="surname" xxf:whitespace="trim"/>
                    <xf:bind id="checkbox-bind" ref="checkbox" name="checkbox" type="xf:boolean"
                             readonly="(../checkbox)"/>
                </xf:bind>
            </xf:bind>
        </xf:bind>

        <!-- Metadata -->
        <xf:instance xxf:readonly="true" id="fr-form-metadata" xxf:exclude-result-prefixes="#all">
            <metadata>
                <application-name>a</application-name>
                <form-name>a</form-name>
                <title xml:lang="en">Untitled Form</title>
                <description xml:lang="en"/>
                <singleton>false</singleton>
            </metadata>
        </xf:instance>

        <!-- Attachments -->
        <xf:instance id="fr-form-attachments" xxf:exclude-result-prefixes="#all">
            <attachments>
                <css mediatype="text/css" filename="" size=""/>
                <pdf mediatype="application/pdf" filename="" size=""/>
            </attachments>
        </xf:instance>

        <!-- All form resources -->
        <!-- Don't make readonly by default in case a service modifies the resources -->
        <xf:instance id="fr-form-resources" xxf:readonly="false" xxf:exclude-result-prefixes="#all">
            <resources>
                <resource xml:lang="en">
                    <surname>
                        <label>Full Name</label>
                        <hint/>
                    </surname>
                    <checkbox>
                        <label>Make him primary applicant</label>
                        <hint/>
                    </checkbox>

                    <section-1>
                        <label>Caregiver</label>
                    </section-1>



                </resource>
            </resources>
        </xf:instance>

        <xf:instance xxf:readonly="true" id="section-1-template" xxf:exclude-result-prefixes="#all">
            <section-1-iteration>
                <surname/>
                <checkbox>false</checkbox>
            </section-1-iteration>
        </xf:instance>

    </xf:model>
</xh:head>
<xh:body>
    <fr:view>
        <fr:body xmlns:xbl="http://www.w3.org/ns/xbl" xmlns:p="http://www.orbeon.com/oxf/pipeline"
                 xmlns:oxf="http://www.orbeon.com/oxf/processors">

            <fr:section id="section-1-section" bind="section-1-bind" repeat="content" min="1"
                        template="instance('section-1-template')"
                        apply-defaults="true"
                        fb:initial-iterations="first">
                <xf:label ref="$form-resources/section-1/label"/>
                <fr:grid id="grid-2-grid">
                    <fr:c x="1" y="1" w="12">
                        <xf:input id="surname-control" bind="surname-bind">
                            <xf:label ref="$form-resources/surname/label"/>
                            <xf:hint ref="$form-resources/surname/hint"/>
                            <xf:alert ref="$fr-resources/detail/labels/alert"/>


                        </xf:input>
                    </fr:c>
                    <fr:c x="1" y="2" w="12">
                        <fr:checkbox-input xmlns="http://orbeon.org/oxf/xml/form-builder"
                                           xmlns:xxbl="http://orbeon.org/oxf/xml/xbl"
                                           id="checkbox-control"
                                           bind="checkbox-bind">
                            <xf:label ref="$form-resources/checkbox/label"/>
                            <xf:hint ref="$form-resources/checkbox/hint"/>
                            <xf:alert ref="$fr-resources/detail/labels/alert"/>


                        </fr:checkbox-input>
                    </fr:c>
                </fr:grid>
            </fr:section>
        </fr:body>
    </fr:view>
</xh:body>

Upvotes: 1

Views: 193

Answers (1)

avernet
avernet

Reputation: 31743

Assuming you named your Single Checkbox field my-checkbox, then you can use the following validation XPath expression:

count(//my-checkbox[string() = 'true']) <= 1 or
string() != 'true'

With this expression, if you have more than one checkbox checked, then all the checked checkboxes will be marked as invalid, thus informing users they must change their selection, and preventing the data from being saved or submitted (if you indeed don't want invalid data to be saved or submitted).

You might also want to add a custom alert message, like "you can't select more than one primary applicant", so users know what the problem is.

Upvotes: 1

Related Questions