Search code examples
jsfdisabled-inputselectoneradio

Disable specific <h:selectOneRadio> items


I have 5 radio buttons in my form, selecting one and proceeding takes me to a form that the user need to fill up. Now out of these 5, I have not yet finished 2 of the forms that these radio buttons take me. So I wanted to disable the radio buttons which donot have the forms ready.

How I solve it now: if a user selects one of these radio buttons that donot have a form ready, it says "Page under construction" but I want to disable them completely so that no one selects it.


Solution

  • If you're using f:selectItems, use the SelectItem constructor taking the disabled argument.

    public class Bean {
        private List<SelectItem> selectItems;
    
        public Bean() {
            selectItems = new ArrayList<SelectItem>();
            selectItems.add(new SelectItem(1, "Form 1", null, false));
            selectItems.add(new SelectItem(2, "Form 2", null, false));
            selectItems.add(new SelectItem(3, "Form 3", null, false));
            selectItems.add(new SelectItem(4, "Form 4", null, true));
            selectItems.add(new SelectItem(5, "Form 5", null, true));
        }
    
        // getter for selectItems field ...
    }
    

    Or, if you're using f:selectItem, use the itemDisabled attribute.

    <f:selectItem itemValue="1" itemLabel="Form 1" itemDisabled="false" />
    <f:selectItem itemValue="2" itemLabel="Form 2" itemDisabled="false" />
    <f:selectItem itemValue="3" itemLabel="Form 3" itemDisabled="false" />
    <f:selectItem itemValue="4" itemLabel="Form 4" itemDisabled="true" />
    <f:selectItem itemValue="5" itemLabel="Form 5" itemDisabled="true" />