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

前端 未结 3 1904
深忆病人
深忆病人 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<UIComponent> children = viewRoot.getChildren();
    
        resetInputValues(children);
    }
    
    private void resetInputValues(List<UIComponent> 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:

    <f:actionListener type="com.cacib.bean.CleanLocalValuesListener"/>
    
    0 讨论(0)
  • 2020-11-22 10:24

    Inside your tag <p:ajax/>, please add an attribute resetValues="true" to tell the view to fetch data again, in this way should be able to fix your problem.

    0 讨论(0)
  • 2020-11-22 10:26

    The cause of the problem can be understood by considering the following facts:

    • When JSF validation succeeds for a particular input component during the validations phase, then the submitted value is set to null and the validated value is set as local value of the input component.

    • When JSF validation fails for a particular input component during the validations phase, then the submitted value is kept in the input component.

    • When at least one input component is invalid after the validations phase, then JSF will not update the model values for any of the input components. JSF will directly proceed to render response phase.

    • When JSF renders input components, then it will first test if the submitted value is not null and then display it, else if the local value is not null and then display it, else it will display the model value.

    • As long as you're interacting with the same JSF view, you're dealing with the same component state.

    So, when the validation has failed for a particular form submit and you happen to need to update the values of input fields by a different ajax action or even a different ajax form (e.g. populating a field depending on a dropdown selection or the result of some modal dialog form, etc), then you basically need to reset the target input components in order to get JSF to display the model value which was edited during invoke action. Otherwise JSF will still display its local value as it was during the validation failure and keep them in an invalidated state.

    One of the ways in your particular case is to manually collect all IDs of input components which are to be updated/re-rendered by PartialViewContext#getRenderIds() and then manually reset its state and submitted values by EditableValueHolder#resetValue().

    FacesContext facesContext = FacesContext.getCurrentInstance();
    PartialViewContext partialViewContext = facesContext.getPartialViewContext();
    Collection<String> renderIds = partialViewContext.getRenderIds();
    
    for (String renderId : renderIds) {
        UIComponent component = viewRoot.findComponent(renderId);
        EditableValueHolder input = (EditableValueHolder) component;
        input.resetValue();
    }
    

    You could do this inside the handleAddressChange() listener method, or inside an reuseable ActionListener implementation which you attach as <f:actionListener> to the input component which is calling the handleAddressChange() listener method.


    Coming back to the concrete problem, I'd imagine that this is an oversight in the JSF2 specification. It would make much more sense to us, JSF developers, when the JSF specification mandates the following:

    • When JSF needs to update/re-render an input component by an ajax request, and that input component is not included in the process/execute of the ajax request, then JSF should reset the input component's value.

    This has been reported as JSF issue 1060 and a complete and reuseable solution has been implemented in the OmniFaces library as ResetInputAjaxActionListener (source code here and showcase demo here).

    Update 1: Since version 3.4, PrimeFaces has based on this idea also introduced a complete and reusable solution in flavor of <p:resetInput>.

    Update 2: Since version 4.0, the <p:ajax> got a new boolean attribute resetValues which should also solve this kind of problem without the need for an additional tag.

    Update 3: JSF 2.2 introduced <f:ajax resetValues>, following the same idea as <p:ajax resetValues>. The solution is now part of standard JSF API.

    0 讨论(0)
提交回复
热议问题