In JSF disabled/readonly field's setters don't get called and hence the backing bean would never have the value of disabled field, be it check box or input text fields etc. What to do if I need value of disabled fields in fields mapped at bean? How can I force setters to get called so that it set the value back? Any suggestion/help will be highly appreciated.
Asked
Active
Viewed 3,249 times
3
-
I am sorry i didnt get this. Can you please elaborate a little more. – Monu Mishra Mar 26 '13 at 07:10
-
@AlexandreLavoie how can you bypass it by using view or session scope, could you give more detail please? – tt_emrah Mar 27 '15 at 09:57
-
@tt_emrah This is because when using RequestScoped, the bean is created to show the page and recreated when you submit (do an action). If you want to keep the first value, you can use ViewScoped ou SessionScoped, so the bean will stay alive during the full scope, so it will keep the precedent value you set. – Alexandre Lavoie Mar 28 '15 at 18:38
-
@AlexandreLavoie yes, i know that, thanks for the details. but how will that make the fields disabled by jsf submitted to backend? – tt_emrah Mar 29 '15 at 08:43
-
@tt_emrah, It will not be, but why would you send a disabled field? The value should not have changed anyway, that's why you can use another scope and get the precedent value. If you really need the field value, the only way would be to get it by JavaScript, or maybe enable the field right before the submit. – Alexandre Lavoie Mar 30 '15 at 17:26
2 Answers
1
You could either disable the fields on client side with javascript or jQuery instead of using JSF's disabled="true"
e.g. with jQuery:
See this post for details: How to disable an input with jQuery?
Or you could submit the disabled input field values as hidden fields together with your form
<h:inputHidden value="#{myBean.myValue}" />

Community
- 1
- 1

Matt Handy
- 29,855
- 2
- 89
- 112
-
Thanks Matt for help. But is there any other way to handle this. I think if i manage to call setters of that field it should be fine. So is there any way i can force setter call? – Monu Mishra Mar 26 '13 at 09:10
-
If you have any action method in your form (e.g. attached to any other enabled input field), you can call the setters from these action methods. – Matt Handy Mar 26 '13 at 09:53
-
You can't submit a disabled element. When JSF determines if it should be processed or not, it sees no use in processing it since it is disabled and its value shouldn't have changed. This can be due to a gap between the actual `ViewState` and the one being managed by JSF. Why do you have a disabled field in first place? why would you want to submit it? My guess is that you have a field that you, somehow, update and you need its new value. Use the scripting approach or take another approach. Forcing setters on disabled fields seems unnecesary (IMO). – Fritz Mar 26 '13 at 16:21
-
Thanks Gamb for suggestion. Yes need of disabled field is excatly what you said. Assume i have many disabled fields which contain values depending on many other fields on same page. Writing scripts to handled them individually would be overhead. I was thinking that on click of button which has action, i would take all fields that are disabled and from view+name i would find their setters and call them. Sounds wierd but i think it is one of the way i can tackle many disabled fields and at the same time have generic code. – Monu Mishra Mar 27 '13 at 13:43
0
You could just make disabled='false'
and use style="display:none"
(or better do that in a css class, of course) in your styling? Thus the field will be processed from a jsf perspective, but will not take space in your view on the browser side.

Stef
- 2,603
- 2
- 18
- 28