I’m trying to render a button that should be initially rendered disabled and at some point enabled by client side actions. I’m using the component from a4j with the following code:
<a4j:commandButton id="myButton" disabled="true" onclick="myFunction()">
The problem is when the disabled attribute is set to true, the events are not attached to the component, resulting in this html code:
<input type="button" onclick="return false" ... />
So when I try to enable via javascript, the button is enabled, but the buttons don’t have the event listeners attached.
So far, the only two solutions I can think about are:
- Assigning the value of the disabled attribute to a bean property and rerender the button.
- Render initially enabled and disabled on the load page through javascript
Both options would work but they are not very clean, I don’t want to make a petition to the server every time I enable the button.
That’s just how stateful component based MVC frameworks like JSF works. As part of safeguard against tampered/hacked requests, the framework re-evaluates the
disabled(andrendered) attribute of an input element whenever it’s about to apply the request values. Otherwise endusers would be able to invoke actions or submit values they’re not allowed to do by server side restrictions which would potentially put doors wide open to attacks.The two solutions which you mentioned are perfectly fine. I’d opt for the first one if you don’t want to allow the enduser to tamper/hack it. It can easily be done by ajax.