字段验证失败时添加全局消息 [英] Add global message when field validation fails

查看:83
本文介绍了字段验证失败时添加全局消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,消息的PrimeFaces标签显示全局消息和特定于字段的消息.我想做的是只显示全局消息(globalOnly="true"),如果任何字段的验证失败,也要显示一条普通消息.无论有多少个字段失败,都只会显示一条消息,例如请更正您的数据".特定于字段的错误已显示在输入字段旁边,因此无需将其显示两次.

Usually the PrimeFaces tag for messages shows the global as well as the field specific messages. What I want to do is to just show the global messages (globalOnly="true") but also show a common message if the validation of any field fails. No matter how many fields fail, just one message like 'Please correct your data'. The field specific errors are already shown next to the input fields so no need to display them twice.

我正在为每个输入(TextboxDropdownRadios,...)使用复合组件.每个字段的验证都应在带有文本框的blur上完成,并且可能在valuechanged的下拉列表和单选框中进行.

I'm using composite components for each of my inputs (Textbox, Dropdown, Radios, ...). The validation of each field should be done on blur with textboxes and probably on valuechanged for dropdowns and radios.

现在我要处理两种类型的验证.首先是JSF本身带来的标准验证器.例如required="true",还有validateRegexvalidateLength,...

Now there are two types of validation I want to handle. First is the standard validators brought by JSF itself. required="true" for example, but also validateRegex, validateLength, ...

然后有一些值我必须对照另一个后端进行检查.对于那些我可能会在我的bean中创建方法并将其称为我的

And then there are values I've to check against another backend. For those I would probably create methods in my bean and call them as listener of my

<p:inputText id="#{cc.attrs.name}"
             value="#{cc.attrs.val}"
             styleClass="#{cc.attrs.iconClass}"
             required="#{cc.attrs.required}"
             requiredMessage="#{cc.attrs.requiredMessage}">
    <p:ajax event="blur" process="@form" update="outer-wrapper" 
            listener="#{cc.attrs.someValidationMethod}" />
</p:inputText>

因此,基本上,如果任何字段验证失败,我只希望收到一条全局消息.我知道可以使用rendered="#{facesContext.validationFailed}"渲染一个附加框,但我希望有一条全局消息.是否有现成的设置或必须实施?

So basically I want to have just one global message if any of the field validations fails. I know could just render an additional box with rendered="#{facesContext.validationFailed}" but I prefer to have a global message. Is there a out-of-the-box setting or does it have to be implemented?

推荐答案

我使用阶段监听器来做到这一点.这大概是我的实现(使用 OmniFaces ):

I use a phase listener to do so. This is roughly my implementation (using OmniFaces):

public class ValidationFailedListener implements PhaseListener
{

  @Override
  public void afterPhase(PhaseEvent event) { }

  @Override
  public void beforePhase(PhaseEvent event) {
    if (Faces.isValidationFailed()) {
      Messages.addGlobalError("Your validation failed message");
    }
  }

  @Override
  public PhaseId getPhaseId() {
    return PhaseId.RENDER_RESPONSE;
  }

}

您应该在faces-config.xml中注册它:

<lifecycle>
  <phase-listener>your.ValidationFailedListener</phase-listener>
</lifecycle>

我的实际实现使用消息包来显示本地化消息.

My actual implementation uses a message bundle to display a localized message.

如果您不能使用OmniFaces,则以下是使用原始JSF的相关代码:

If you cannot use OmniFaces, here is the relevant code using vanilla JSF:

if (FacesContext.getCurrentInstance().isValidationFailed()) {
  FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                          "Your validation failed message",
                                          null);
  FacesContext.getCurrentInstance().addMessage(null, message);
}

这篇关于字段验证失败时添加全局消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆