I do not understand the behaviour of JSF2 during valdation. Hope someone can help me.
I have a form where the fields are validated after (ajax) submit - ok
Aaron already descripes the behaviour.
The problem I've been described exists also by clicking the 'newContact' button. If the first submit is not valid (birthday was entered, name-field is empty) an error message is shown. ok.
Afterwards the 'newContact' Button do not refresh (clear) the view. Although the model was resetted (contact = new Contact()).
I found some tipps here: http://wiki.apache.org/myfaces/ClearInputComponents
Here is my solution:
public void newContact(ActionEvent ae) {
contact = new Contact();
contact.setBirthday(new Date()); //for testing only
resetForm(ae.getComponent());
}
private void resetForm(UIComponent uiComponent) {
//get form component
UIComponent parentComponent = uiComponent.getParent();
if (uiComponent instanceof UIForm)
resetFields(uiComponent);
else if (parentComponent != null)
resetForm(parentComponent);
else
resetFields(uiComponent);
}
private void resetFields(UIComponent baseComponent) {
for (UIComponent c : baseComponent.getChildren()) {
if (c.getChildCount() > 0)
resetFields(c);
if (c instanceof UIInput)
((UIInput) c).resetValue();
}
}