0
<h:form>
            <h:selectOneMenu value="#{productBean.productName}">
                <f:selectItem itemValue="" itemLabel="..." />
                <f:selectItems value="#{productBean.pizza}" var="pizza" itemValue="#{pizza.name}" itemLabel="#{pizza.name}" />
                <f:ajax listener="#{productBean.valueChanged(productBean.productName)}" render="pizzaResult pizzaButton" />
            </h:selectOneMenu>
            <h:commandButton value ="Dodaj do zamówienia" disabled="#{productBean.isDisabled}"  id="pizzaButton" onclick="#{productBean.order}"/>
            <h:outputText id="pizzaResult" value="#{productBean.message}" />
        </h:form>

This is my JSF form. I used valueChanged listener to make button diabled in ome cases and it works good. But I don't get why it triggers also the buttons onclick. How to do something which enable me to use button ONLY after clicking it?

I noticed that when I delete the disabled option it works good:/ But why I cannot trigger the action when button is enabled in the moment?

Vasil Lukach
  • 3,658
  • 3
  • 31
  • 40
Mateusz Gaweł
  • 673
  • 1
  • 8
  • 22
  • "But I don't get why it triggers also the buttons onclick" - I don't see an `onclick` listner on your button! – Niks May 12 '13 at 16:04
  • Also, a relevant point is - you don't need the other `` to render `pizzaButton` You could just add that to your previous one as `render="pizzaResult pizzaButton"` – Niks May 12 '13 at 16:08
  • `How to do something which enable me to use button ONLY after clicking it`, you want your button can use after one click ? – Rong Nguyen May 13 '13 at 01:32
  • #1 I copied wrong code and I wrote the onclick manually in the wrong place. It's ok now #2 Thanks for advice, I did what u said and it works good. #3 I want button that do some action after clicking it but the action cannot be triggered by ajax listener which is onchange. – Mateusz Gaweł May 13 '13 at 08:09
  • possible duplicate of [h:commandLink / h:commandButton is not being invoked](http://stackoverflow.com/questions/2118656/hcommandlink-hcommandbutton-is-not-being-invoked) (point 5) – BalusC May 14 '13 at 11:25

2 Answers2

0

onclick is a client side attribute so you shouldn't try to bind it to managed bean method calls and it looks as though you did onclick=#{productBean.order} (apart from the missing quotes). This may be the cause of your problems.

dratewka
  • 2,104
  • 14
  • 15
  • I noticed that just now, you're right :) In fact, I had completely misunderstood the question! – Niks May 12 '13 at 15:40
  • Well, I'm not 100% sure myself, but looks like this could be it – dratewka May 12 '13 at 15:42
  • i added it manually in the stackoverflow textfield because i copied it from my IDE without the onlick. It's with " " – Mateusz Gaweł May 12 '13 at 18:10
  • But actually the quotes are not the problem - the problem is, that `onclick` is a client side callback - so it should be set to some javascript, not a bean method. – dratewka May 12 '13 at 18:15
  • I don't want to use JS here. What is need is only the action which will be triggered ONLY after clicking the button. Now it's also triggered by ajax listener. How should i do that? – Mateusz Gaweł May 13 '13 at 08:10
  • Ok, which action do you have I mind - the ajax listener triggers `productBean.valueChanged`, so do you want to invoke it after the `commandButton` gets clicked? – dratewka May 13 '13 at 08:38
  • no. Another method. Listener set flag disabled on true or false. Another method from the button should trigger the getter method. PLease read my self answer to the topic. – Mateusz Gaweł May 13 '13 at 08:50
0

OK i did that. The problem is that the button may appear as enabled (disabled="false") but its client side change and server don't know about it and application thinks that the button is still disabled. Even it looks like enabled button it won't work with action="#{something}". You have to let server know about the change. THe only thing i did is adding the @ViewScoped to the managed bean. Now the action of disabling and enabling the button is also being seen by the server and works perfectly.

However i have a question. Client side verifaction is a bad idea. Disabled button is the only thing which prevent user from sending empty itemValue or product(in my case) which is unavailable (is_available = 0 in DB). The question is: Is it enough to ensure that it will be safe?

edit: Unfortunately after clicking the button the buttons appear enabled even though the oneSelectMenu is turned to the first, empty value. After changing the list it works again as previously, and after clicking again the situation takes time again.

Mateusz Gaweł
  • 673
  • 1
  • 8
  • 22