0

I have a switch (boolean).And when switch is true,inputtext disabled option is true.When switch is false,inputtext disabled option is false and so I can input data.But this data returns null.

    <h:form id="form">
                <p:inputSwitch value="#{switchMB.isTrue}" onLabel="True"
                    offLabel="False">
                    <p:ajax update="inputText" />
                </p:inputSwitch>

                <p:inputText id="inputText" value="#{switchMB.inputTextValue}"
                    disabled="#{switchMB.isTrue}"  />

                <p:commandButton action="#{switchMB.addValue}" 
                    update=":form" value="Add"></p:commandButton>
    </h:form>

when I input the data in inputText,this data always returns null.How can I solve this problem? How to update disabled option in inputText and input the data correctly?thanks in advance..

erginduran
  • 1,678
  • 5
  • 28
  • 51

1 Answers1

2

As part of safeguard against tampered/hacked requests, the disabled attribute is also evaluated during apply request values phase of the form submit. So basically you need to make sure that #{switchMB.isTrue} evaluates exactly the same as it did during rendering the form. The easist way is to put the #{switchMB} managed bean in the view scope instead of in the request scope.

@Named // Or @ManagedBean
@ViewScoped // Watch out that you import from the right package!
public class SwitchMB {}

Namely, when you put it in the request scope, then all properties would be reset to their defaults, which in your particular case apparently triggers a disabled="true".

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • thanks.It's work.But I have a getList method for dataTable in the same managedBean.When I add new switched inputText (data) to db(so to datatable from this db),I update/request dataTable list.So if I use viewscoped,list of datatable is not up to date.So I should use RequestScoped or I create new managedBean for only this switch operation.What should I do?is this the only option (viewscoped)?Thanks again. – erginduran Mar 10 '15 at 21:53
  • Then make sure in your action you also explicitly update the list in the viewscoped bean – Kukeltje Mar 10 '15 at 22:56