Search code examples
jsfprimefacesfacelets

Change Update attribute dynamicaly in Primefaces


I am looking for a way to change the update attribute of a primefaces command button from the backing bean after the form submit. What i am trying to achieve is to update the component id's based on the results from backing bean method.

For example, i am trying to update the form and the growl message with a command button, now if some error has happened from the backing bean(not validation error), i need to update only the growl message and the form shouldn't be updated.

<p:commandButton value="Finish Editing"
        action="#{editBean.finish}" icon="ui-icon-check"
        style="width:200px;margin-left:60px;" update=":studentEditForm   :messageForm:applyMessages" />

Solution

  • You can use the programmatic API via RequestContext#update().

    public void finish() {
        // ...
    
        if (someCondition) {
             RequestContext.getCurrentInstance().update("someClientId");
        } else {
             RequestContext.getCurrentInstance().update("otherClientId");
        }
    }             
    

    Don't forget to remove the update attribute from the command button.