JSF 2 - Bean Validation: validation failed -> empty values are replaced with last valid values from managed bean

前端 未结 4 1168
一向
一向 2020-12-05 08:18


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

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 09:14

    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();
        }
    }
    

提交回复
热议问题