0

I have a <p:selectOneMenu> component, that is fed with an Array of instances of some Class (say ClassABC). In the top of the selectItems list, I want to provide a <f:selectItem> that will serve as a Wildcard/Any option, and it's value should be a null object.

I managed to make this work as

<p:selectOneMenu  
        value="#{someBean.selectedInstance}"
        converter="classABCDConverter" >
    <!--  Wildcard Value -->
    <f:selectItem itemLabel=" * Any * " itemValue="" />

    <!-- Dynamic Data with Real Instances -->
    <f:selectItems 
        var="result"
        value="#{someBean.arrayOfInstancesOfClassABCD}"
        ....
         />
</p:selectOneMenu>

So far this seems to work, as selecting the -any- select item, gives a null Object to someBean.selectedInstance. I guess that itemValue="" makes the Converter return a null object, so it probably looks legit.

But I'm just curious if this is the safest and most efficient way to go.

EDIT: Alternatively I could use noSelectionOption="true" so, for example, which would be better from the 2 bellow :

<f:selectItem itemLabel=" * Any * " itemValue="" />

or

<f:selectItem itemLabel=" * Any * " noSelectionOption="true" />
yannicuLar
  • 3,083
  • 3
  • 32
  • 50
  • At least this one works for hundreds of selectOneMenus for me, so I do hope that this is a safe way to go :-) – Dominik Sandjaja Jul 12 '13 at 09:48
  • 1
    possible duplicate of [Best way to add a "nothing selected" option to a selectOneMenu in JSF](http://stackoverflow.com/questions/11360030/best-way-to-add-a-nothing-selected-option-to-a-selectonemenu-in-jsf) – Aritz Jul 12 '13 at 09:56
  • 2
    you also have a `noSelectionOption="true"` in `` – Eric C. Jul 12 '13 at 10:21
  • thanx for the tip on noSelectionOption. I was pretty much searching for something like that. But is this declaration safer than 'itemValue=""' – yannicuLar Jul 12 '13 at 10:27
  • @Xtreme Biker : right, I missed that question, although I gave some time searching before posting my question – yannicuLar Jul 12 '13 at 10:29
  • 2
    The only difference I think is that `noSelectionOption` doesn't call the converter while `itemvalue=""` does. In summary, `noSelectionOption` is the way to go, the JSF standard one. – Aritz Jul 12 '13 at 10:32
  • 1
    If you use a `required="true"` in the ``, it is safer to go with `noSelectionOption` since it will fail validation and show a "required" message (if you use it). Like @XtremeBiker said, it will not invoke the converter and avoid returning an empty string or something like that... – Eric C. Jul 12 '13 at 12:11

0 Answers0