java <p:messages> 摘要和详细信息中的重复文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13467881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:52:56  来源:igfitidea点击:

Duplicate text in <p:messages> summary and detail

javajsfprimefacesmessagesgrowlnotify

提问by Seitaridis

When the email address is invalid the message displayed is "Invalid email.Invalid email.". I know that the message has two parts: summary and a details. I need both of these, but I want to have different info in each. How can I change the message to display "Invalid email: Please enter a valid email address" instead?

当电子邮件地址无效时,显示的消息是“电子邮件无效。电子邮件无效。”。我知道该消息有两部分:摘要和详细信息。我需要这两个,但我希望每个都有不同的信息。如何更改消息以显示“无效的电子邮件:请输入有效的电子邮件地址”?

<p:messages showDetail="true" autoUpdate="true" closable="true" />
<h:panelGrid columns="2">
    <h:outputText value="#{label.email}: *" />
    <p:inputText required="true" value="#{userWizard.emailAddress}"
        validatorMessage="#{label.invalidEmail}" label="#{label.email}">
        <f:validateRegex pattern="^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$">
        </f:validateRegex>
    </p:inputText>
</h:panelGrid>  

回答by BalusC

This is not possible with validatorMessage(neither converterMessagenor requiredMessage). The value will be used as boththe summary and the detail.

这是不可能的validatorMessage(既不是converterMessage也不是requiredMessage)。该值将被用作总结和细节。

You'd need to homegrow a custom validator instead wherein you can construct the FacesMessagewith the both parts yourself. Assuming that you've next to the label.emailalso a label.email_detailwhich represents the message detail, then it should look something like this:

你需要homegrow一个自定义的验证,而不是其中您可以构建FacesMessage与两个部分自己。假设您旁边label.email还有label.email_detail代表消息详细信息的 a ,那么它应该如下所示:

@FacesValidator("emailValidator")
public class EmailValidator implements Validator {

    private static final Pattern PATTERN = Pattern.compile("([^.@]+)(\.[^.@]+)*@([^.@]+\.)+([^.@]+)");

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        if (value == null || ((String) value).isEmpty()) {
            return; // Let required="true" handle.
        }

        if (!PATTERN.matcher((String) value).matches()) {
            String summary = context.getApplication().evaluateExpressionGet(context, "#{label.email}", String.class);
            String detail = context.getApplication().evaluateExpressionGet(context, "#{label.email_detail}", String.class);
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, detail));
        }
    }

}

(note that I adapted the email regexp for better preparation on world domination; non-latin characters like Chinese, Hebrew, Cyrillic, etc are namely allowed in domain names and thus also email addresses since the new IANA decision in 2010)

(请注意,我修改了电子邮件正则表达式,以便更好地为统治世界做好准备;自2010 年 IANA 做出决定以来,域名和电子邮件地址中都允许使用中文、希伯来语、西里尔文等非拉丁字符)

which is then to be used as

然后将其用作

<p:inputText ... validator="emailValidator" />

回答by Alexandre Lavoie

According to the documentation here : http://www.primefaces.org/docs/vdl/3.4/primefaces-p/messages.html

根据这里的文档:http: //www.primefaces.org/docs/vdl/3.4/primefaces-p/messages.html

You could do something like that :

你可以这样做:

<p:messages showSummary="true" showDetails="true" />

You can also put them apart... for styling :

您也可以将它们分开...用于造型:

<p:messages showSummary="false" showDetails="true" />
<p:messages showSummary="true" showDetails="false" />

But you can't define the two error messages with validatorMessage :

但是您不能使用 validatorMessage 定义两个错误消息:

http://www.primefaces.org/docs/vdl/3.4/primefaces-p/inputText.html

http://www.primefaces.org/docs/vdl/3.4/primefaces-p/inputText.html