JSF valueChangeListener fired before UpdateModel phase

The other day I was trying to use a JSF valueChangeListener to compare 2 passwords fields for equality.

The bean code.

1
2
3
4
5
6
7
8
9
10
@In
private Map<String, UIComponent> uiComponent;

//Change listener to make sure passwords match
public void verifyPasswordMatches(ValueChangeEvent e) {
  verifyPassword = (String) e.getNewValue();
  UIComponent password = uiComponent.getSubmittedValue("registration:passwordField:password");
 
  //Compare the 2 fields.
}

The 2 fields in the view.

1
2
<h:inputSecret value="#{register.password} id="password" />
<h:inputSecret value="#{register.verifyPassword} valueChangeListener="{register.verifyPasswordMatches}" id="verifyPassword"/>

No matter what I tried I couldn’t get the value from the password field. After scratching my head for a while I finally figured out why. It is because the valueChangeListener is fired before the UpdateModel phase. This means the model is still empty which explains why the password field was blank. The only field that has a value is the field that has the valueChangeListener.

The morale of the story…remember the JSF life cycle.



Comments

No comments yet.

Add Yours

  • Author Avatar

    YOU


Comment Arrow




About Author

Brian

Brian Abston is a Java Developer in Oklahoma who also loves technology.