问题 单个输入字段的自定义JSF验证器消息


我想为不同的输入字段的每个验证器提供不同的验证消息。

在JSF中是否可以为单个验证器提供不同的验证消息(例如 <f:validateLongRange>)对于每个输入字段?


7240
2017-09-26 11:29


起源



答案:


有几种方法:

  1. 最简单,只是设置 validatorMessage 属性。

    <h:inputText ... validatorMessage="Please enter a number between 0 and 42">
        <f:validateLongRange minimum="0" maximum="42" />
    </h:inputText>
    

    但是,当您使用其他验证器时也会使用此选项。不确定那会不会形成问题。如果是这样,请采取以下方式。

  2. 创建一个扩展标准验证器的自定义验证器,例如 LongRangeValidator 在你的情况下,抓住 ValidatorException 并使用所需的自定义消息重新抛出它。例如。

    <h:inputText ...>
        <f:validator validatorId="myLongRangeValidator" />
        <f:attribute name="longRangeValidatorMessage" value="Please enter a number between 0 and 42" />
    </h:inputText>
    

    public class MyLongRangeValidator extends LongRangeValidator {
    
        public void validate(FacesContext context, UIComponent component, Object convertedValue) throws ValidatorException {
            setMinimum(0); // If necessary, obtain as custom attribute as well.
            setMaximum(42); // If necessary, obtain as custom attribute as well.
    
            try {
                super.validate(context, component, convertedValue);
            } catch (ValidatorException e) {
                String message = (String) component.getAttributes().get("longRangeValidatorMessage");
                throw new ValidatorException(new FacesMessage(message));
            }
        }
    
    }
    
  3. 使用 OmniFaces  <o:validator> 允许在每个验证器的基础上设置不同的验证器消息:

    <h:inputText ...>
        <o:validator validatorId="javax.faces.Required" message="Please fill out this field" />
        <o:validator validatorId="javax.faces.LongRange" minimum="0" maximum="42" message="Please enter a number between 0 and 42" />
    </h:inputText>
    

也可以看看:


13
2017-09-26 11:36



我会选择2号,因为我需要不同的消息来解决不同的验证错误。您的解决方案批准我担心我需要超出标准行为的解决方案。 - Grolsch


答案:


有几种方法:

  1. 最简单,只是设置 validatorMessage 属性。

    <h:inputText ... validatorMessage="Please enter a number between 0 and 42">
        <f:validateLongRange minimum="0" maximum="42" />
    </h:inputText>
    

    但是,当您使用其他验证器时也会使用此选项。不确定那会不会形成问题。如果是这样,请采取以下方式。

  2. 创建一个扩展标准验证器的自定义验证器,例如 LongRangeValidator 在你的情况下,抓住 ValidatorException 并使用所需的自定义消息重新抛出它。例如。

    <h:inputText ...>
        <f:validator validatorId="myLongRangeValidator" />
        <f:attribute name="longRangeValidatorMessage" value="Please enter a number between 0 and 42" />
    </h:inputText>
    

    public class MyLongRangeValidator extends LongRangeValidator {
    
        public void validate(FacesContext context, UIComponent component, Object convertedValue) throws ValidatorException {
            setMinimum(0); // If necessary, obtain as custom attribute as well.
            setMaximum(42); // If necessary, obtain as custom attribute as well.
    
            try {
                super.validate(context, component, convertedValue);
            } catch (ValidatorException e) {
                String message = (String) component.getAttributes().get("longRangeValidatorMessage");
                throw new ValidatorException(new FacesMessage(message));
            }
        }
    
    }
    
  3. 使用 OmniFaces  <o:validator> 允许在每个验证器的基础上设置不同的验证器消息:

    <h:inputText ...>
        <o:validator validatorId="javax.faces.Required" message="Please fill out this field" />
        <o:validator validatorId="javax.faces.LongRange" minimum="0" maximum="42" message="Please enter a number between 0 and 42" />
    </h:inputText>
    

也可以看看:


13
2017-09-26 11:36



我会选择2号,因为我需要不同的消息来解决不同的验证错误。您的解决方案批准我担心我需要超出标准行为的解决方案。 - Grolsch