在必需的属性和绑定中添加多个条件



我想创建一个JSF应用程序。很抱歉这个过于雄心勃勃的要求。在应用程序中,用户将有三个下拉列表。

我们可以将多个必需条件添加到一个必需属性中吗?

如果用户从第一个下拉列表中选择国家墨西哥/值3,则需要从多选中选择选项坎昆/值6

此外,如果他选择下拉值北美大陆,他需要从多选中选择芝加哥/值2选项

我已经为多个所需选项添加了multiSelectListbox的绑定、所需属性,但当下拉菜单选择时,它们不会被触发

<h:form>
<p:growl id="msgs" showDetail="true" skipDetailIfEqualsSummary="true" />
<p:panel header="Tranfer Destination" style="margin-bottom:10px;">
<h:panelGrid columns="2" cellpadding="5">
<p:outputLabel for="country" value="Country: " />
<p:selectOneMenu id="country" value="#{dropdownView.country}" style="width:150px" required="true" binding="#{country}">
<f:selectItem itemLabel="Select Country" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{dropdownView.countries}" />
<p:ajax update="city"/
</p:selectOneMenu>
<p:outputLabel for="Continent" value="Continent: " />
<p:multiSelectListbox id="continent" value="#{dropdownView.continent}" style="width:150px" required="true" binding="#{continent}">
<f:selectItem itemLabel="Select Continent" itemValue="" noSelectionOption="true" />
<f:selectItem itemLabel="Asia" itemValue="1"> </f:selectItem>
<f:selectItem itemLabel="Europe" itemValue="2"> </f:selectItem>
<f:selectItem itemLabel="South America"  itemValue="3"> </f:selectItem>
<f:selectItem itemLabel="North America" itemValue="4"> </f:selectItem>
<f:selectItem itemLabel="Africa" itemValue="5"> </f:selectItem>
<p:ajax update="city"/
</p:multiSelectListbox>
<p:outputLabel for="city" value="City: " />
<p:multiSelectListbox id="city" value="#{dropdownView.city}" style="width:150px" required="#{(not empty param[country.6])  (not empty param[continent.4])}">
<f:selectItem itemLabel="Select City" itemValue="" noSelectionOption="true" />
<f:selectItem itemLabel="New York" itemValue="1"> </f:selectItem>
<f:selectItem itemLabel="Chicago" itemValue="2"> </f:selectItem>
<f:selectItem itemLabel="Seattle"  itemValue="3"> </f:selectItem>
<f:selectItem itemLabel="Toronto" itemValue="4"> </f:selectItem>
<f:selectItem itemLabel="Ontario" itemValue="5"> </f:selectItem>
<f:selectItem itemLabel="Cancun" itemValue="6"> </f:selectItem>
<f:selectItem itemLabel="Tijuana" itemValue="7"> </f:selectItem>
</p:multiSelectListbox>
</h:panelGrid>
<p:separator />
<p:commandButton value="Submit" update="msgs" action="#{dropdownView.displayLocation}" icon="pi pi-check" />
</p:panel>
</h:form>

国家列表由数据库填充

ID NAME
1 USA-C
2 Canada-N
3 Mexico-C

仅当用户从第一个下拉列表中选择国家墨西哥/值3时,第三个下拉列表才需要选项Cancun/value 6。

仅当用户从第二个下拉列表中选择北美大陆/值4时,第三个下拉列表才需要选项Chicago/value 2。

其他城市可供选择

如果必须这样做,我将不使用required属性,我将创建一个自定义验证器,检查在第一个和第二个下拉列表中选择的选项,并查看第三个下拉列表是否具有上一个选择所需的值。

https://www.primefaces.org/showcase/ui/csv/custom.xhtml

最新更新