PrimeFaces: conditional update on validation

后端 未结 2 975
轮回少年
轮回少年 2021-01-22 06:49

Is it possible to conditionally update JSF components only when validation succeeds?

I would like to be able to do something like



        
相关标签:
2条回答
  • 2021-01-22 07:30

    I don't think the message suggestion necessarily answers the question asked. Suppose he wants to update something OTHER than a message?

    I've not tried this myself, but another approach that might work is to use remotecommand.

    <p:remoteCommand id='good' update='goodUpdates'/>  
    <p:remoteCommand id='bad' update='badUpdate'/>  
    <p:commandButton oncomplete='if (your-test) good() else bad()'/>  
    

    Note, this would necessitate another round-trip to the server and thus performance is a consideration.

    0 讨论(0)
  • 2021-01-22 07:37

    The <h:message> (or PrimeFaces' counterpart <p:message>) is intented for this. Or, in your case maybe better, the <h:messages> (or <p:messages>).

    public void submit() {
        // ...
    
        if (fail) {
            FacesContext.getCurrentInstance().addMessage(null, 
                new FacesMessage(FacesMessage.SEVERITY_ERROR, "Fail", null));
        }
    }
    

    with

    <h:messages id="messages" />
    <p:commandLink process="@form" action="#{bean.submit}" update="messages something" />
    

    Note that you're as well supposed to use a normal Validator implementation to perform the validation. If it throws a ValidatorException, then the action won't be invoked anyway. Doing validation inside action method is a smell.

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