validatorMessage override other message

时间:2016-05-17 11:18:33

标签: validation jsf jsf-2

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);
    }
}

1 个答案:

答案 0 :(得分:-1)

正如您已经注意到“validatorMessage覆盖其他消息”。

f:validateRegex对于消息定制也不是很方便。

  1. 删除属性:validatorMessage
  2. 删除标记:f:validateRegex
  3. 通过在bean中添加followin注释,移动到容易定制的Bean Validation和i18n:
  4. 示例代码:

    @Pattern(regexp = "[A-Za-z\\. ]+", message = "{pattern.regex.phoneMessage}")
    private String phoneNo;
    

    需要在以下内容中定义消息:ValidationMessages.properties用于默认语言环境,在ValidationMessages_xx.properties中用于“xx”语言环境。

    看一下很棒的article