Duplicate text in <p: messages> summary and detail
If the email address is invalid, the message "Invalid email.Invalid email." Is displayed. I know that a message consists of two parts: a summary and details. I need both, but I want to have different information in each. How can I change the message to display "Invalid email address: 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> This is not possible with validatorMessage (neither converterMessage nor requiredMessage ). The value will be used for both the summary and the part.
Instead, you will need to create your own validator, instead of which you can build FacesMessage from both parts yourself. Assuming there is also a label.email_detail next to label.email that provides detailed information, then it should look something like this:
@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 regular expression of email to better prepare for world domination, and not Latin characters like Chinese, Hebrew, Cyrillic, etc. are allowed in domain names and therefore also email addresses, as new IANA solution in 2010 )
which should then be used as
<p:inputText ... validator="emailValidator" /> According to the documentation here: http://www.primefaces.org/docs/vdl/3.4/primefaces-p/messages.html
You can do something like this:
<p:messages showSummary="true" showDetails="true" /> You can also separate them ... for styling:
<p:messages showSummary="false" showDetails="true" /> <p:messages showSummary="true" showDetails="false" /> But you cannot define two error messages using validatorMessage:
http://www.primefaces.org/docs/vdl/3.4/primefaces-p/inputText.html