Search code examples
primefacesjsf-2

Check box value is coming as false if it is disabled in primefaces


I have a requirement like this.

there is a file uploader alopng with checkbox and save, cancel buttons.

I have to disable the check box if the file is invalid and the selection should stay.

After disabling if I click the save button the checkbox value is coming as false in backing bean eventhough it's showing as checked in the dialog.

While debugging I seen that the primeface framework is not getting the id of the disabled checkbox,

Here is my xhtml file:

    <h:form id="fileupload_form" enctype="multipart/form-data">
    <h:panelGroup id="fileupload_Panel">
    <p:message for="fileupload_Panel"></p:message>

    <p:panelGrid styleClass="noBorderGrid global_docs" columns="4">

        <h:outputText style="min-width:100px!important; margin-left: 42px;"></h:outputText>
        <p:selectBooleanCheckbox id="tt" value="#{bean.selectedDocs}"/>
        <h:outputText value="xx}"/>


        <h:outputText style="min-width:100px!important; margin-left: 42px;" rendered="#{cc.attrs.bean != null and cc.attrs.bean.currentDocument != null}"></h:outputText>
        <p:selectBooleanCheckbox id="checkBox" value="#{fileBean.tqpReport}"  disabled="${not fileBean.isActive}"/>
        <h:outputText value="xxxx"/>
        <p:tooltip for="t" value="xxxx"/>

    </p:panelGrid>

    <p:fileUpload id="documentFileUploader" fileUploadListener="#{bean.uploadDocumentsListener}" 
                    mode="advanced" dragDropSupport="true" multiple="true" auto="true"  update="@form" process="@form"
                    onstart="PF('ajax').show();"  onerror="PF('ajax').hide();"
                    style="width: 98%; max-width: 480px; max-height: 150px; overflow: auto;" styleClass="file-uploader-drag-drop" label="#{msgs.choose}" />
    </h:panelGroup>


     <p:panelGrid styleClass="noBorderGrid buttonTable" columns="3" style="float:right;">
         <p:commandButton id="save" icon="ui-icon-disk" styleClass="blue_button" value="${msgs.save}" style="float:right;"
        process="@form" update="@form" action="#{bean.save()}"/>
         <p:commandButton id="cancel" icon="ui-icon-close" styleClass="blue_button" value="${msgs.cancel}"
        action="#{bean.cancel()}"  style="float:right;"
        process="@this"/>
</p:panelGrid> 

 </h:form>

While debugging I found that in the

   public void decode(FacesContext context, UIComponent component)

method of

   SelectBooleanCheckboxRenderer.java class 

the id of the disabled check box is null from that statement. I don't understand why? If the check box is not disabled I am getting the correct value.

  String submittedValue = (String) context.getExternalContext().getRequestParameterMap().get(clientId + "_input"); 

Solution

  • I tried with hidden variable trick, it's working for me

    hidden variable in xhtm file, and getters and setter in backing bean.

    <h:inputHidden id="checkBoxSelection" value="#{fileBean.checkBoxSelection}"/>
    

    While saving, I am getting the selection,and setting to the hidden variable like,

     <p:commandButton id="save" icon="ui-icon-disk" styleClass="blue_button" value="${msgs.save}" style="float:right;"
        process="@form" update="@form" action="#{fileBean.saveUploadedDocuments(cc.attrs.id)}" 
     onclick="sendCheckBoxSelection('fileupload_form_widget_tqpReportCheckBox','fileupload_form:checkBoxSelection');"/>
    

    and the js function is:

    function sendCheckBoxSelection(checkBoxId,hiddenparamId){
    var checkBoxSelection = false;
    var tqpCheckBox = getWidgetVarById(checkBoxId);
    if (tqpCheckBox != null){
        checkBoxSelection = PrimeFaces.widgets[checkBoxId].input.is(':checked')
        document.getElementById(hiddenparamId).value = checkBoxSelection;
      }
    }
    
     function getWidgetVarById(id) {
    for (var propertyName in PrimeFaces.widgets) {
      if (propertyName === id) {
        return PrimeFaces.widgets[propertyName];
       }
      }
    }
    

    So, the checkbox value will set to the backing bean while saving .

    http://blog.hatemalimam.com/intro-to-primefaces-widgetvar