How to update p:selectOneRadio value through component binding

2k Views Asked by At

I have bound a p:selectOneRadio component as below,

<p:selectOneRadio binding="#{bean.maritalStatusRadio}" value="#{bean.maritalStatus}" >
    <f:selectItems value="#{selectItemList.maritalStatusList}" /> 
    <p:ajax event="change" listener="#{bean.handleChangeInMaritalStatus}" />
</p:selectOneRadio>

But I am unable to update its value from backing bean class.

SelectOneRadio maritalStatusRadio; 
    public void handleChangeInMaritalStatus() {
            String currentValue =String.valueOf(maritalStatusRadio.getValue());

            if(!currentValue.equals(MARITAL_STATUS_MARRIED)) {
                maritalStatusRadio.setValue(MARITAL_STATUS_MARRIED);

            }

    }`
2

There are 2 best solutions below

0
On

Your listener method is not the place to change your select value. It's for doing whatever you need when the user changes selected option in the view.

If you want to change the value selected you simply need to change the value assigned to your bound field, in this case bean.maritalStatus. You can do it in many places (including youtr listener if required).

Usually on preRenderView method is the place to initialize a select default value. On your view:

  <f:metadata>
    <f:event
      type="preRenderView"
      listener="#{bean.initializeForNew}" />
  </f:metadata>

On your bean:

 public void initializeForNew() throws ModelException {
    if (!isPostBack()) {//only on loading
      bean.maritalStatus = getDefaultMaritaStatus();
    }
  }
2
On
<p:selectOneRadio id="radioButton" binding="#{nominee.maritalStatusRadio}" value="#{nominee.prospective.maritalStatus}"  disabled="#{disabled}" >
                                    <f:selectItems value="#{selectItemList.maritalStatusList}" />
                                    <p:ajax update="radioButton" event="change" listener="#{nominee.handleChangeInMaritalStatus}" />
                            </p:selectOneRadio>
It worked for me I was not updating p:selectOneRadio in  p:ajax