Search code examples
javajsf-2primefacesspring-webflow

Why are these PrimeFaces radio buttons disabled?


I have what I think is some simple code:

<ui:composition template="/templates/webflow.xhtml"
            xmlns="http://www.w3.org/1999/xhtml"
            xmlns:h="http://java.sun.com/jsf/html"
            xmlns:ui="http://java.sun.com/jsf/facelets"
            xmlns:p="http://primefaces.org/ui"
            xmlns:f="http://java.sun.com/jsf/core">

<ui:define name="flowTitle">Page One</ui:define>
<ui:define name="pageTitle">Page 1 of 5: Please select a type</ui:define>

<ui:define name="content">
    <h:form id="page1Form">
        <div class="flow_main">
            <div class="controls">
                <p:selectOneRadio id="customRadio" value="#{flowState.selectedOption}" layout="custom">
                    <f:selectItem itemValue="-1" />
                    <f:selectItem itemValue="1" />
                    <f:selectItem itemValue="2" />
                    <f:selectItem itemValue="3" />
                    <f:selectItem itemValue="4" />
                    <f:selectItem itemValue="5" />
                    <f:selectItem itemValue="6" />
                </p:selectOneRadio>

                <!-- these to go into a datatable -->
                <p:radioButton id="opt2" for=":page1Form:customRadio" itemIndex="1"/>
                <p:radioButton id="opt3" for=":page1Form:customRadio" itemIndex="2"/>
                <p:radioButton id="opt4" for=":page1Form:customRadio" itemIndex="3"/>
                <p:radioButton id="opt5" for=":page1Form:customRadio" itemIndex="4"/>
                <p:radioButton id="opt6" for=":page1Form:customRadio" itemIndex="5"/>
                <p:radioButton id="opt1" for=":page1Form:customRadio" itemIndex="6"/>

            </div>
        </div>
        <div style="text-align:right" class="flow_footer" >
            <p:commandButton id="cancel" value="Cancel" ajax="true" action="cancel" onsuccess="javascript:parent.dlg.hide();"/>
            <p:commandButton id="next" action="next" value="Next" onclick="return validate(1)" update="@form" />
        </div>
    </h:form>
</ui:define>

However, when rendered the 3 bottom radio buttons are disabled. I've tried changing just about all of the attributes to make them enabled but to no avail.

The only way I have managed to get them enabled is by adding this line:

<h:inputHidden value="#{flowState.selectedOption}" id="hiddenId" />

Even this doesn't work fully though; selecting a radio button and going to the next page binds correctly. However when I go back a page the radio buttons are disabled again!


Solution

  • Just in case it's useful to anyone else, the reason my radio buttons were disabled was due to a javascript error being thrown from a seemingly unrelated section of code:

    <ui:define name="content">
        <script>
            jQuery(document).ready(function() {
                var selected = document.getElementById("page1Form:hiddenId").value;
                if(selected != -1) {
                    document.getElementById("page1Form:typeTable:" + selected + ":chkType").checked = true;
                }
            });
        </script>