当电子邮件地址无效时,显示的消息是“电子邮件无效。电子邮件无效。”。我知道消息有两部分:摘要和细节。我需要这两个,但我想在每个中都有不同的信息。如何将消息更改为显示“无效的电子邮件:请输入有效的电子邮件地址”?
<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>
最佳答案
validatorMessage
(converterMessage
和requiredMessage
)不可能做到这一点。该值将用作摘要和详细信息。
您需要自己开发一个自定义 validator ,您可以在其中构建 FacesMessage
和 您自己 的两个部分。假设 label.email
旁边还有一个代表消息详细信息的 label.email_detail
,那么它应该看起来像这样:
@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));
}
}
}
(请注意,我调整了电子邮件正则表达式,以便更好地准备统治世界;自新的 IANA decision in 2010 以来,域名和电子邮件地址中都允许使用非拉丁字符,例如中文、希伯来语、西里尔字母等。 )
然后用作
<p:inputText ... validator="emailValidator" />
关于java - <p :messages> summary and detail 中的重复文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13467881/