jsfjsf-2.2selectoneradiopassthrough-elements

Preset checked state of h:selectOneRadio with passthrough elements


I am trying to implement the solution described in <h:selectOneRadio> renders table element, how to avoid this?

<div class="form-group">
    <h:outputLabel styleClass="control-label"
        value="#{msgClient.legalPersonality} " />
    Selected value : <h:outputText value="#{msgClient['legalPersonality.' += clientBean.clientDto.legalPersonality.label]}" />
    <div>                                       
        <f:metadata>
            <f:viewParam name="legalPersonality" value="#{clientBean.clientDto.legalPersonality}" />
        </f:metadata>
        <ui:repeat var="legalPersonality" value="#{clientBean.legalPersonalities}">
            <label class="radio-inline">
                <input type="radio" jsf:id="legal" pt:name="legalPersonality" value="#{legalPersonality}">
                    <f:ajax execute="@this" render="@form" />
                </input> 
                <h:outputText value="#{msgClient['legalPersonality.' += legalPersonality.label]}" />
            </label>                                        
        </ui:repeat>
    </div>
</div>

Everything seems to work fine, the selected value is correctly updated with the f:ajax component (setter is called with the correct value) BUT radio buttons are never checked. I mean it is checked when I click on it but it goes unchecked immediatly after. Even at page load, with a default value in clientDto.legalPersonality, everything is unchecked.

How it looks like : http://oi58.tinypic.com/242788g.jpg

Any help will be greatly appreciated. Thank you.


Solution

  • Indeed, the example in the question you found was a bit too oversimplified. It was missing the checked attribute which is necessary in order to preset the current selection on page load and/or after ajax render as in your case. In HTML, the checked state of a radio button is achieved by setting the checked attribute.

    You can use <f:passThroughAttribute> to set the checked attribute, whereby you make sure that you set #{null} in unchecked case, so that it wouldn't render the checked attribute at all. It's namely a minimized attribute; even if we used checked="false", HTML would still consider it checked.

    <input type="radio" jsf:id="legal" pt:name="legalPersonality" value="#{legalPersonality}">
        <f:passThroughAttribute name="checked" value="#{legalPersonality eq clientBean.clientDto.legalPersonality ? 'checked' : null}" />
        <f:ajax execute="@this" render="@form" />
    </input> 
    

    The example in the question you found has in the meanwhile been updated. Thanks!