Reputation: 12876
I have two drop downs on a page whose backing bean is Request Scoped. The second drop down is always disabled when the page is first rendered. However, if the user switches the first drop down to a different value than the default, the second drop down is enabled. When the user submits the form, I want my RequestScoped managed bean to see the value of the second drop down. However, evidently, becuase the second drop down was initially disabled, its value is not submitted with the form, even though when the form was submitted, the dropdown was enabled.
The way I have worked around this is to store the disabled "state" of the second drop down in a ViewScoped bean. That way, when the user changes the first drop down to a value other than the default, an actionListener can fire which will update the value of the ViewScoped bean associated with the second dropdown (changing its state to not disabled).
Is this the best way to deal with this situation? Or is there a better way?
Upvotes: 2
Views: 706
Reputation: 1274
I had a similar issue. I eventually solved it using a "fake" disabled.
<div style="position:relative">
<p:inputText styleClass="ui-state-disabled">
<!-- fake-disable input field -->
<div style="position:absolute; top:0; left:0; width:100%; height:100%;"/>
</div>
Using a invisible div above the input the user can't interact with it. Additionally, I styled the input field so that it looks disabled.
Upvotes: 0
Reputation: 1108922
That's indeed the most sane approach.
An alternative, if you really need to have your bean in the request scope, is to re-determine the disabled
state of the second dropdown in the (post)constructor of the bean based on the submitted value of the first dropdown which you can find in the request parameter map (or just let JSF set it by @ManagedProperty
). You only need to manually repeat the JSF conversion/validation if necessary.
Let's assume that the second dropdown should not be disabled anymore when the submitted value of the first dropdown equals the string value foo
and that the client ID of the first dropdown is form:dropdown1
:
public Bean() {
this.dropdown2disabled = !"foo".equals(FacesContext.getCurrentInstance()
.getExternalContext().getRequestParameterMap().get("form:dropdown1"));
}
But as you found out, a view scoped bean is easier.
As again another alternative, you could use Tomahawk's <t:saveState>
to store a single property directly in the view state. Add this somewhere to the view then:
<t:saveState value="#{bean.dropdown2disabled}" />
This will result in a smaller size of the view state than when using a view scoped bean.
Upvotes: 2