4

This is my ManagedBean:

@Named(value = "mrBean")
@RequestScoped
public class MrBean {

   public void laugh() {
      System.out.println("HAHAHA");
   }

   public void prepareToLaugh() {
      System.out.println("Drink water.");
   }

}

And this is the working version of my commandButton:

<p:commandButton actionListener="#{mrBean.laugh}" widgetVar="laughtButton"
                 value="Laugh" oncomplete="laughButton.disable();"  />

When I clicked the above button, I saw HAHAHA and the button is disabled. However, when I set the laughButton's disable attribute to true, the button does not work anymore:

<p:commandButton actionListener="#{mrBean.laugh}" widgetVar="laughtButton"
                 value="Laugh" disabled="true" oncomplete="laughButton.disable();"  />

<p:commandButton actionListener="#{mrBean.prepareToLaugh}"
                 value="Prepare to laugh" oncomplete="laughButton.enable();" />

When I click the 2nd button, I saw Drink water and the 1st button is enabled. However, when I click on the 1st button, nothing happens.

I'd be very grateful if someone could give me an advice on how I should tackle this problem. I'm using PrimeFaces 3.0 RC2.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Mr.J4mes
  • 9,168
  • 9
  • 48
  • 90
  • @JigarJoshi: I'm not sure what you mean by "directly make it enable". In brief, if I don't initially set the `disabled` to true, it works. If I do, it doesn't work. =) – Mr.J4mes Dec 27 '11 at 17:23

1 Answers1

9

Like as with rendered attribute, JSF re-evaluates the disabled attribute in the server side during processing of the form submit as part of safeguard against tampered requests and like. You're however enabling/disabling it by JS without notifying the server side of the changes.

You need to ensure that the value of the disabled attribute evaluates false during the request whenever you intend the button to be enabled during processing of the form submit. For example, bind it to a boolean property of a view scoped bean which is set by the other button.

<h:commandButton disabled="#{bean.laughDisabled}" />

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I have always been using this way. However, I saw PrimeFaces's client-side API earlier and I thought that I could save a boolean property. Turned out it's still needed :P. Thanks! – Mr.J4mes Dec 27 '11 at 17:26
  • 1
    API is more like for visual helper for push buttons. – Cagatay Civici Dec 27 '11 at 17:55