我有一个使用JSF2的PrimeFaces实现。我使用的是<p:selectOneRadio />
组件。当用户在无线电组件中选择“否”时,我想显示一条自定义消息。为此,我创建了一个自定义验证器,与消息组件一起,一切都很好。但是,也需要该组件。我不希望所需组件的"Value is required“验证错误出现在消息组件中。在这种情况下,我只希望突出显示字段和标签。
因此,问题是:如何指定给定的<p:message />
组件只显示来自自定义验证器的错误,而不显示来自所需验证器的错误?
几年前,我看到了一个类似问题的答案:您可以将message for
属性设置为实际上不存在的组件,并通过FacesContext将消息添加到该组件中。比如:<p:message for="fooMsg" />
. FacesContext.getCurrentInstance().addMessage("fooMsg",msg)
然而,这似乎行不通。JSF抛出一个无法找到组件"fooMsg“的错误。
这是我目前的密码。
构成部分:
<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>
验证器:
@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);
}
}
}
}
发布于 2018-03-22 23:12:49
首先,这是默认的JSF行为。为了解决您的问题,您可以使用jQuery检查是否选择了单选按钮,并在这种情况下隐藏消息,例如:
<h:form id="form">
<p:outputLabel for="foo" id="foolbl" value="Some label text." />
<br />
<p:message id="foomsg" 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 process="@this" update="@this foolbl foomsg" />
<f:validator validatorId="FooValidator" />
</p:selectOneRadio>
<p:commandButton process="@this foo" update="foo foolbl foomsg"
oncomplete="if( !PF('fooVar').checkedRadio.length ) $('#form\\:foomsg').hide();" />
</h:form>
查看一下commandButton的oncomplete属性。
https://stackoverflow.com/questions/49438023
复制