1

I have an h:selectBooleanCheckbox which I'd like to validate inside the managed bean before allowing it to change it's value.

<h:selectBooleanCheckbox id="attr" value="#{handler.attribute}" onclick="submit()" 
    immediate="true"  valueChangeListener="#{handler.changeAttributeValue}" />

public String changeAttributeValue(ValueChangeEvent event) {
    if(condition)
        attribute=false;
return "home";
}

So what I'd like to do is prevent the attribute from becoming true if the condition is true. What happens is the attribute is set to false at first but after the method exits it becomes true again.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Mihnea Mihai
  • 181
  • 1
  • 3
  • 18

1 Answers1

2

Be careful with immediate="true". The submitted value won't be taken by your attribute but stay in the component model of the selectBooleanCheckbox- component, so the box is checked again next time. Try immediate=false. Be sure, that your Managed Bean is @SessionScoped, otherwise it will forget your attribute as you reload/redirect the page.

Even more handsome:

<h:selectBooleanCheckbox id="attr" value="#{handler.attribute}">
    <f:ajax event="change" render="@this" listener="#{handler.changeAttributeValue()}" />
</h:selectBooleanCheckbox>

public void changeAttributeValue() {
if(condition)
    attribute=false;
return;
}

(Couldn't check the code right know, but used this quite often)

Dawn
  • 126
  • 6