Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional update in Primefaces/jsf

First of all, so that there are no misunderstandings, while this is the same question as PrimeFaces: conditional update on validation I am asking it again, because the answer to that question is not acceptable to me, because while it may have given the original poster a workaround for what they were asking, it doesn't really answer the question, and this workaround doesn't work for me.

So, here's my question: how do I conditionally update a primefaces component based on the results of the submition?

I have a component that must not, under any circumstances be updated UNLESS the validation is successful and backend code was executed successfully. (i.e. if the validation succeeds but there was an SQL exception on the back end, the component still shouldn't be updated).

Why can't I always update it? If the logic doesn't succeed it'll be updated to the same thing it was before the submit button was clicked. Because I can't. It's the captcha component. If you update it via ajax, it disappears, end of story. There was a ticket opened at Primefaces and they closed it as won't fix, because flash compoents aren't supposed to be updated w/ ajax.

So when I submit the form, unless the logic succeeds I need to leave the captcha alone. IF the logic succeeds, I need to make sure the captcha disappears off screen. What's the easiest, cleanest way to do it?

Excuse me if this is a n00b question. I'm really new to Primefaces and JSF.

like image 589
Creature Avatar asked Dec 17 '12 16:12

Creature


Video Answer


3 Answers

RequestContext.getCurrentInstance().update("clientId")

Helps for conditional updates.

For recaptcha try

RequestContext.getCurrentInstance().execute("Recaptcha.destroy()");
like image 98
Cagatay Civici Avatar answered Oct 18 '22 18:10

Cagatay Civici


You can use a combination of JavaScript and RemoteCommand:

<p:commandLink action="#{bean.doIt()}" process="@form" update="@none" oncomplete="if(!args.validationFailed){updateMyImportantUI();}" />

<p:remoteCommand name="updateMyImportantUI" process="@this" update=":myImportantID" />     
like image 30
moxprox Avatar answered Oct 18 '22 19:10

moxprox


You can use a variable at the backing bean and use it on the xhtml part.
Like this:

update="#{backingMB.updateString}"

So, after the condition you can put the value for updateString, but when you define it on the backingMB, you need to put it as:

String updateString=""; // this will be id of the component to be updated  

This way you won't get a NullPointerException.

like image 2
javacihaci Avatar answered Oct 18 '22 19:10

javacihaci