0

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

heidi
  • 11
  • 2
  • Does this answer your question? [How to update a selectonemenu depending on other one selected value?](https://stackoverflow.com/questions/13338861/how-to-update-a-selectonemenu-depending-on-other-one-selected-value) – fuggerjaki61 Mar 01 '20 at 17:09
  • Sorry , It does not. Here in the second drop down we are making sure that it is selected/required item for selection. – heidi Mar 02 '20 at 01:58
  • There is no required attribute at all on the second `p:selectOneMenu`. Try adding one and make it 'conditional' – Kukeltje Mar 02 '20 at 06:14
  • Does this answer your question? [Conditionally make inputs required depending on checkbox value](https://stackoverflow.com/questions/19143684/conditionally-make-inputs-required-depending-on-checkbox-value) – Kukeltje Mar 02 '20 at 06:18
  • Sorry , It did not work – heidi Mar 03 '20 at 03:55

0 Answers0