Question

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.

Was it helpful?

Solution

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top