1

Can someone tell me why the valueChangeListener method is never fired?

  <h:form>
   <h:selectBooleanCheckbox
    value="#{reaCtrl.update}"
    valueChangeListener="#{reaCtrl.updateC}" onclick="submit()"> 
    </h:selectBooleanCheckbox>
  </h:form>

The method updateC looks like that:

public void updateC(ValueChangeEvent event) {
         System.out.println("testC");
}

Thx

Tarik
  • 4,961
  • 3
  • 36
  • 67
Yassine
  • 29
  • 1
  • 5
  • How did you know that the method is never fired?! i tried your code and its perfectly working! how did you declared the update variable? – Tarik Feb 09 '15 at 00:20
  • The text 'testC" does not print. The variable update is private boolean. – Yassine Feb 09 '15 at 19:01
  • I just tested again your code and its working! are you running Tomcat? wich version? did you looked in the console? please add your entire xhtml and bean code. – Tarik Feb 10 '15 at 00:44
  • Please, http://stackoverflow.com/help/mcve The current code snippet is not that. When anyone else in the world copypastes that into a blank playground project running on most recent versions of the libraries with everything set to default, then it works just fine. – BalusC Feb 10 '15 at 08:00
  • 1
    Well guys here is what I found, the checkbox is customized by some java script code to make it look "nicer". When I remove this js code, everything works normally. Any idea why this js code is causing problems? – Yassine Feb 11 '15 at 09:45
  • 1
    @Yassine what are you talking about? are you asking about something we have ever seen and you want someone to reply ?! you are asking if a Javascript code will work without providing it, how could we know ?! – Tarik Feb 12 '15 at 01:20

2 Answers2

2

Your updateC method will be invoked by the JSF implementation after the Process Validation Phase, so my guess is that you have some other validation errors which causes that your method is never invoked.

You can place <h:messages/> tag in start of your form to display all conversion / validation errors.

However, in case you want to bypass that validation, you can use the immediate attribute like this:

<h:selectBooleanCheckbox
value="#{reaCtrl.update}" immediate="true"
valueChangeListener="#{reaCtrl.updateC}" onclick="submit()"> 
</h:selectBooleanCheckbox>

Adding that attribute will fire a Value Change events after Apply Request Values Phase, which means that your method updateC will be invoked in that phase, after the method xecutes you should skip to the Render Response Phase, so you need to modify your method like this:

public void updateC(ValueChangeEvent event) {
         System.out.println("testC");
         FacesContext context = FacesContext.getCurrentInstance();
         context.renderResponse();
}
Tarik
  • 4,961
  • 3
  • 36
  • 67
-1

Do like this:

<h:selectBooleanCheckbox value="#{reaCtrl.update}"> 
    <p:ajax event="change" partialSubmit="true" process="@this"/>
</h:selectBooleanCheckbox>