Make a multiSelect drop downvalue required based in the input of previous p:selectOneMenu value

别说谁变了你拦得住时间么 提交于 2021-02-11 14:18:10

问题


I want to create a JSF application. In the application the user will have two drop downs.

If a user selects the country Mexico / value 3 from the first drop down then he is required to choose the option Cancun / value 6 from the multiselect How this can be implemented?

<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">
            <f:selectItem itemLabel="Select Country" itemValue="" noSelectionOption="true" />
            <f:selectItems value="#{dropdownView.countries}" />
        </p:selectOneMenu>

        <p:outputLabel for="city" value="City: " />
        <p:multiSelectListbox id="city" value="#{dropdownView.city}" style="width:150px">
            <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>

List of Countries are populated from the database

ID NAME

  1. USA-C
  2. Canada-N
  3. Mexico-C

List of cities

  • New york (optional)
  • Chicago (optional)
  • Seattle (optional)
  • Toronto (optional)
  • Ontario (optional)
  • Cancun [required]
  • Tijuana (optional)

Here in the second drop down we are making sure that the option is selected/required item for selection

Update

I have made necessary changes as suggested but it does not work.

<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:selectOneMenu>

    <p:outputLabel for="city" value="City: " />
    <p:multiSelectListbox id="city" value="#{dropdownView.city}" style="width:150px" required="#{not empty param[country.6]}">
        <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>

The the option Cancun / value 6 is required from the second drop down only If the user selects the country Mexico / value 3 from the first drop down

来源:https://stackoverflow.com/questions/60476673/make-a-multiselect-drop-downvalue-required-based-in-the-input-of-previous-psele

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!