My email input field has two validator, the first one uses f:validateRegex, the second one use f:validator. Default message displayed when validateRegex was fail ('value is invalid'), validator message displayed when validator was fail ('email is already in use'). It is fine until I customize f:validateRegex message with validatorMessage. Input field now display the new message ('email is invalid') for both which is unexpected.
So How can I keep f:validator message?
HTML:
<p:inputText id="phoneNo" value="#{bean.phoneNo}"
validatorMessage="#{msg['errors.phone.invalid']}">
<f:validateRegex pattern="#{msg['pattern.regex.phone']}" />
<f:validator binding="#{phoneNoValidator}" />
</p:inputText>
<p:message for="phoneNo" showDetail="true" />
Validator class:
public void validate(FacesContext context, UIComponent component,
Object value) throws ValidatorException {
fc = context;
String phoneNo = String.valueOf(value);
if (driverBO.isExistMobileNo(phoneNo )) {
FacesMessage message = getErrorMessage(Errors.PHONE_DUPLICATED);
throw new ValidatorException(message);
}
}
答案 0 :(得分:-1)
正如您已经注意到“validatorMessage覆盖其他消息”。
f:validateRegex对于消息定制也不是很方便。
示例代码:
@Pattern(regexp = "[A-Za-z\\. ]+", message = "{pattern.regex.phoneMessage}")
private String phoneNo;
需要在以下内容中定义消息:ValidationMessages.properties用于默认语言环境,在ValidationMessages_xx.properties中用于“xx”语言环境。
看一下很棒的article。