0

I am doing a password editor. The password can be entered in two ways: manually entering the values for password and confirmation or by selecting some already generated password.

To use the generated password, a new value from the select box must be chosen. The change triggers the filling of the password/confirmation fields(psw1 and psw2) with the value from the selected value.

<p:selectOneMenu value="#{password.selectedPassword}" >
    <f:selectItems value="#{password.passwords}" var="val"
        itemLabel="#{val}" itemValue="#{val}" />
        <p:ajax update="psw1, psw2" listener="#{password.passwordChanged}"/>
</p:selectOneMenu>

I need to implement the filling of password fields also when the the same value is selected. How can I implement this? One way is to add an extra value, a default empty value.

Seitaridis
  • 4,459
  • 9
  • 53
  • 85
  • What do you mean by _I need to implement the filling of password fields also when the the same value is selected_? – patstuart Sep 20 '13 at 18:27
  • @patstuart: when the currently selected value (i.e. the default value) is re-selected. – BalusC Sep 20 '13 at 18:29

1 Answers1

1

You can't trigger a change event if the value has not changed. Indeed, one way is to supply a default value as in "Please select" with a #{null} value or even with noSelectionOption="true" in flavor of an additional <f:selectItem>. This forces the enduser to actually change the value to a valid value.

E.g.

<p:selectOneMenu value="#{password.selectedPassword}" >
    <f:selectItem itemValue="#{null}" itemLabel="--select--" />
    <f:selectItems value="#{password.passwords}" var="val"
        itemLabel="#{val}" itemValue="#{val}" />
        <p:ajax update="psw1, psw2" listener="#{password.passwordChanged}"/>
</p:selectOneMenu>

Another way is to use <p:selectOneListbox> instead, which is maybe better if you don't have many items.

<p:selectOneListbox value="#{password.selectedPassword}" >
    <f:selectItems value="#{password.passwords}" var="val"
        itemLabel="#{val}" itemValue="#{val}" />
        <p:ajax update="psw1, psw2" listener="#{password.passwordChanged}"/>
</p:selectOneListbox>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • To add, FYI: I've run into a bug in the past trying to get `noSelectItem` to work for ``. I don't know if the bug is in PrimeFaces or MyFaces 2. – patstuart Sep 20 '13 at 18:42
  • @patstuart: indeed: http://stackoverflow.com/questions/14110755/pselectonemenu-preselects-previous-item-when-noselectionoption-item-is-present/ Bug was in PF by the way. – BalusC Sep 20 '13 at 18:46
  • You overtook me! Congratulations on being number 3 – Marc Gravell Sep 21 '13 at 12:16