0

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"); 
trapzerapzerix
  • 135
  • 2
  • 10
Naveen Kocherla
  • 399
  • 9
  • 27
  • If I understand your question correctly, your checkbox is in disabled state and value is coming as false as disabled fields are not submitted on submit button and default value is false. – Sumit Gulati Mar 31 '17 at 03:14

2 Answers2

0

Disabled elements are not posted as part form submission; you can see this answer for some more details. Assuming that you're using a ViewScoped bean you can use AJAX to post the value back to the bean when the value changes.

Community
  • 1
  • 1
axemoi
  • 211
  • 1
  • 8
-1

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

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Naveen Kocherla
  • 399
  • 9
  • 27