How can I populate a text field using PrimeFaces AJAX after validation errors occur?

前端 未结 3 1974
深忆病人
深忆病人 2020-11-22 09:43

I have a form in a view which performs ajax partial processing for autocompletion and gmap localization. My backing bean instantiates an entity object \"Address\" and is to

3条回答
  •  半阙折子戏
    2020-11-22 10:23

    As BalusC explained, you can also add a reusable listener that cleans all input values, for instance:

    public class CleanLocalValuesListener implements ActionListener {
    
    @Override
    public void processAction(ActionEvent actionEvent) throws AbortProcessingException {
        FacesContext context = FacesContext.getCurrentInstance();
        UIViewRoot viewRoot = context.getViewRoot();
        List children = viewRoot.getChildren();
    
        resetInputValues(children);
    }
    
    private void resetInputValues(List children) {
        for (UIComponent component : children) {
            if (component.getChildCount() > 0) {
                resetInputValues(component.getChildren());
            } else {
                if (component instanceof EditableValueHolder) {
                    EditableValueHolder input = (EditableValueHolder) component;
                    input.resetValue();
                }
            }
        }
      }
    }
    

    And use it whenever you need to clean your local values:

    
    

提交回复
热议问题