I need to enable a commandButton when two required selectOneMenu components have not empty values
This is my view:
<p:selectOneMenu converter="omnifaces.SelectItemsConverter" id="countries" required="true" value="#{countryBean.selectedCountry}">
<f:selectItem itemLabel="#{msg['choose.option']}" />
<f:selectItems value="#{countryBean.countries}" var="country" itemLabel="#{country.name}" />
<f:ajax listener="#{countryBean.changeCountry}" render="cities" update="findPeopleButton" />
</p:selectOneMenu>
<p:selectOneMenu converter="omnifaces.SelectItemsConverter" id="cities" required="true" value="#{countryBean.city}">
<f:selectItem itemLabel="#{msg['choose.option']}" />
<f:selectItems value="#{countryBean.cities}" var="city" itemLabel="#{city.name}" />
<f:ajax listener="#{countryBean.verifyRequiredFields}" update="findPeopleButton" />
</p:selectOneMenu>
<p:commandButton icon="ui-icon-search"
disabled="#{countryBean.disablePeopleButton}"
id="findPeopleButton"
onclick="peopleDialog.show()" />
I disabled the button when my view loads, then when I select values for each selectOneMenu the button enables but after that when I set an empty value of some selectOneMenu I get a required validation error message and the button keeps enabled
This is my backing bean:
private Country country; // +getter+setter
private City city; // +getter+setter
private List<Country> countries; // +getter
private List<City> cities; // +getter
private boolean disablePeopleButton; // +getter+setter
@EJB
private CountryService countryService;
@PostConstruct
public void init() {
disablePeopleButton = true;
countries = countryService.getCountries();
}
public void changeCountry() {
cities = countryService.getCities(country);
}
public void verifyRequiredFields() {
if (country != null && city != null) {
disablePeopleButton = false;
} else {
disablePeopleButton = true;
}
}
What can I do to fix it?