Fant has given a hint in the right direction, that you should be using an enum which has a required
property, but it seems that you're not entirely sure how to properly implement it. Admittedly, Fant's answer is not elaborate enough. So here's a more elaborate answer.
Basically, you need to replace all dropdown values by an enum which look like this:
public enum PaymentType {
FOO("Some label for foo", true),
BAR("Some label for bar", false),
BAZ("Some label for baz", true);
private String label;
private boolean required;
private PaymentType(String label, boolean required) {
this.label = label;
this.required = required;
}
public String getLabel() {
return label;
}
public boolean isRequired() {
return required;
}
}
And use it as follows
<h:selectOneMenu binding="#{selectedPaymentType}" value="#{bean.selectedPaymentType}">
<f:selectItems value="#{bean.availablePaymentTypes}" var="paymentType"
itemValue="#{paymentType}" itemLabel="#{paymentType.label}" />
</h:selectOneMenu>
<h:inputText ... required="#{selectedPaymentType.value.required}" />
with
private PaymentType selectedPaymentType; // +getter+setter
public PaymentType[] getAvailablePaymentTypes() {
return PaymentType.values();
}
(or if you're using OmniFaces, use <o:importConstants>
, then you don't need such a getter for the <f:selectItems>
; no, you don't need a converter in any case, JSF/EL has already builtin conversion for enums)
See, the required
attribute is now so much more simplified as it's already definied in the model associated with the selected value.