2
votes

I have a p:selectOneMenu whose first element is a string 'Please select' and other items are a list to be selected. Can I disable the submit button when 'Please select' is selected and enable the button if other elements are selected? I know that itemDisabled can be used, but that is not my requirement.

<p:selectOneMenu value="#{bean.value}" id="selectId"
    filter="true" required="true" converter="itemConverter"
    filterMatchMode="contains" height="120">
    <f:selectItem itemLabel="Please select"/>
    <f:selectItems value="#{listBean.selectItemList}"></f:selectItems>
</p:selectOneMenu>

Essentially, I want the submit button to be disabled when the first item is selected.

Can someone please help?

2

2 Answers

5
votes

You better replace

<f:selectItem itemLabel="Please select"/>

With

<f:selectItem noSelectionOption="true" itemLabel="Please select"></f:selectItem>

And let the jsf to handle the required for you...


If you really want to disable the button just use the #{bean.value}

like this

<p:commandButton id="myButonId" disabled="#{empty bean.value}"....

and add <p:ajax update="myButonId" to your <p:selectOneMenu

1
votes

Hello Rajath, Why cant you use a SelectItemList in the backing bean and add label and values to that list from backingbean ? The advantage of doing that is, you can add "please Select "as the first label in the SelectItemList and value as null. So for disabling SubmitButton you can have a boolean flag which can be set to true or false according to the values in SelectItemList( use a valuechangeListener and update the boolean flag accordingly in that method ). Hope this solution will help you.