문제

In my jsff file I have some input fields which are required by default. Now we want a way to toggle the required attribute using a checkbox. Several solutions have been tested, one of them like this:

<af:selectBooleanCheckbox id="sbc1" label="myLabel" value="#{sessionBean.skipInput}" autoSubmit="true"/>

<af:selectOneChoice label="myLabel" id="soc1" partialTriggers="sbc1" value="#{sessionScope.sessionBean.someValue}" required="#{!sessionBean.skipInput}">

<f:selectItems value="#{applicationScope.applicationBean.myItems}" id="si1"/>

</af:selectOneChoice>

When I check the checkbox to set the required attribute to false, the selectOneChoice will be red indicating that no value has been selected in the drowdown. How can I prevent this and simply remove the required attribute from the dropdown?

도움이 되었습니까?

해결책

Your solution is the right approach, but the issue is that ADF can not change the checkbox value as long as the required constraint of the choice box is not fulfilled. So you will always get the "A selection is required" message and the new value of the check box will not be updated on the server as long as there is nothing selected in the choice box.

To solve this, you need to set the immediate property on the checkbox to true to skip validation, and add a valueChangeListener to handle the changes of the checkbox and manually call the render response phase:

<af:selectBooleanCheckbox id="sbc1" label="Skip Choice" 
                          value="#{sessionBean.skipInput}" 
                          autoSubmit="true"
                          immediate="true" 
                          valueChangeListener="#{sessionBean.toggleSkipInput}"/>
public class SessionBean {

...

  public void toggleSkipInput(ValueChangeEvent vce) {
    setSkipInput(Boolean.TRUE.equals(vce.getNewValue()));
    FacesContext.getCurrentInstance().renderResponse();
  }
}

For more information, see

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top