Reputation: 4424
I have two entities in JSF that are dropdowns. If i am selelcting month then i should compulsarily select year. If i have not selected Year and only selected month then a validation error should appear on click of submit button. Is such form of conditional validation possible in primefaces. I had tried javascript method but i failed somehow. How can it be acheived using primefaces.? Below are my two entities.
<p:outputLabel for="year" value="Year: " />
<p:selectOneMenu id="year" style="width:150px " value="#{bean.year}">
<f:selectItem itemLabel="All" itemValue="All"/>
<f:selectItem itemLabel="2014" itemValue="2014"/>
<f:selectItem itemLabel="2013" itemValue="2013"/>
<f:selectItem itemLabel="2012" itemValue="2012"/>
<f:selectItem itemLabel="2011" itemValue="2011"/>
<f:selectItem itemLabel="2010" itemValue="2010"/>
<f:selectItem itemLabel="2009" itemValue="2009"/>
</p:selectOneMenu>
<p:outputLabel for="month" value="Month: " />
<p:selectOneMenu id="month" style="width:150px" value="#{bean.month}">
<f:selectItem itemLabel="All" itemValue="All"/>
<f:selectItem itemLabel="January" itemValue="01"/>
<f:selectItem itemLabel="February" itemValue="02"/>
<f:selectItem itemLabel="March" itemValue="03"/>
<f:selectItem itemLabel="April" itemValue="04"/>
<f:selectItem itemLabel="May" itemValue="05"/>
<f:selectItem itemLabel="June" itemValue="06"/>
<f:selectItem itemLabel="July" itemValue="07"/>
<f:selectItem itemLabel="August" itemValue="08"/>
<f:selectItem itemLabel="September" itemValue="09"/>
<f:selectItem itemLabel="October" itemValue="10"/>
<f:selectItem itemLabel="November" itemValue="11"/>
<f:selectItem itemLabel="December" itemValue="12"/>
</p:selectOneMenu>
Upvotes: 0
Views: 1910
Reputation: 41
I think that this simple solution for your case, and if you want to use Custom validator, you have to follow steps:
Create a validator class by implementing the
javax.faces.validator.Validator
interface.
Override validate()
method.
Assign an unique validator ID via @FacesValidator
annotation.
Reference custom validator class to JSF component via f:validator tag.
You can go here for more information Custom validator in JSF 2.0
Upvotes: 1
Reputation: 41
Add the attribute required="true" to your selectOneMenu of year
<p:selectOneMenu id="year" style="width:150px" required="true" value="#{bean.year}">
<!-- some code here -->
</p:selectOneMenu>
<p:message for="year" display="icon" />
note that the tag message is for showing where the error has happened .for the selectOneMenu of month do the same thing ..
Upvotes: 1