I have a JSF2 implementation using PrimeFaces. I am using a <p:selectOneRadio />
component. When the user selects "No" in the radio component, I would like to show a custom message. I have created a custom validator for this purpose, and along with the message component everything works fine. However, the component is also required. I do not want the "Value is required" validation error for the required component to show up in the message component. In this case, I only want the field and the label to be highlighted.
So, the question is: How do I specify that a given <p:message />
component display only the error from my custom validator, and not the error from the required validator?
I saw an answer to a similar question from a few years ago that said you could set the message for
attribute to a component that doesn't actually exist, and add the message to that component via the FacesContext. Such as this:
<p:message for="fooMsg" />
... FacesContext.getCurrentInstance().addMessage("fooMsg",msg)
However, that does not seem to work. JSF throws an error that it cannot find the component "fooMsg".
Here is my current code.
Component:
<p:outputLabel for="foo" id="foolbl"
value="Some label text." />
<br />
<p:message for="foo" />
<p:selectOneRadio id="foo" layout="pageDirection"
widgetVar="fooVar" required="true">
<f:selectItem itemLabel="Yes" itemValue="Yes" />
<f:selectItem itemLabel="No" itemValue="No" />
<p:ajax update="@this foolbl" process="@this" />
<f:validator validatorId="FooValidator" />
</p:selectOneRadio>
Validator:
@FacesValidator("FooValidator")
public class FooValidator implements Validator {
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
if (value != null) {
String sValue = (String) value;
if (sValue.trim().equalsIgnoreCase("No")) {
FacesMessage msg = new FacesMessage("Summary",
"Detail");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(msg);
}
}
}
}