I'm defining some bean validation messages for later i18n. This message should validate that an input is of type number / BigDecimal:

class Payment {
    @Digits(message = "test error msg", fraction = 2, integer = 13)
    private BigDecimal amount;
}

<p:inputText value="#{payment.amount}" />

Unfortunately, if I try to input a text, I do not get my defined msg, but:

'asd' must be a signed decimal number

Could someone explain why? Or rather, how I can force my input field to always validate with the @Digits?

有帮助吗?

解决方案

In JSF validation happens after conversion. If you input something that's not a number in the text field that's mapped to a BigDecimal in the backing bean, JSF will try to convert that into a BigDecimal and it will fail, of course. So the Hibernate Validator won't really have the chance to kick in. You'll need to localize the JSF conversion messages to achieve what you want.

I am not familiar with the @Digits annotation. But the Hibernate Validator docs seem to indicate that it verifies if the number is using the expected amount of integer and fraction digits. So instead of inputting a text in the text field, try inputting a value that is not in the same format accepted by the validator (in your case, fraction = 2, integer = 13). So try inputting the value 123.123120 and see if you get the expected message.

Here's the hack to go all the way into the entities and get the annotations at the converter. First you need to create a custom converter for a number (in this case, BigDecimal):

@FacesConverter("MyBigDecimalConverter")
public class MyBigDecimalConverter extends NumberConverter {
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
    try {
        HtmlInputText it = (HtmlInputText) arg1;
        ValueExpression ve = it.getValueExpression("value");
        String expression = ve.getExpressionString();
        String field = expression.replaceAll("#\\{.*\\.", "");
        field = field.replace("}", "");
        String parent = expression.replace("." + field, "");
        ExpressionFactory expressionFactory = arg0.getApplication().getExpressionFactory();
        ValueExpression exp = expressionFactory.createValueExpression(arg0.getELContext(), parent, Object.class);
        Object obj = exp.getValue(arg0.getELContext());

        Digits d = null;    
        Field f = obj.getClass().getDeclaredField(field);
        d = f.getAnnotation(Digits.class);
        return super.getAsObject(arg0, arg1, arg2);
    } catch (ConverterException e) {
        FacesMessage msg = new FacesMessage(d.message());
        msg.setSeverity(FacesMessage.SEVERITY_ERROR);
        throw new ConverterException(msg);
    } catch (Exception e) {
        return super.getAsObject(arg0, arg1, arg2);
    }
}

@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
    return super.getAsString(arg0, arg1, arg2);
}

}

And you need to use that converter in your page:

    <h:outputText value="#{someBean.someEntity.nome}" />
    <h:form>
        <p:inputText value="#{someBean.someEntity.someBigDecimal}" id="test">
            <f:converter converterId="MyBigDecimalConverter" />
        </p:inputText>
        <h:message for="test"></h:message>
        <p:commandButton value="teste" process="@form" update="@form"></p:commandButton>
    </h:form>

I am using primeFaces components here, but you don't really need them.

What exactly is the converter doing? It is fetching the object in your bean that hold the value that the inputText refers to and the field name. Then, it finds the @Digits annotation for that field and reads its message. It asks for the NumericConverter to convert the String into a number. If that fails, it uses the message in the @Digits annotation for the error message.

其他提示

You should use <f:validateBean /> to delegates validation to the Bean Validation API (JSR 303)

<p:inputText value="#{payment.amount}" >
    <f:validateBean />
</p:inputText>

Not sure about this <p:inputText> tag but in vanilla JSF you could just:

<h:inputText id="price"
    value="#{movieController.movie.price}" redisplay="true"
    converterMessage="Please input a decimal">
    <f:ajax event="blur" render="numberOfCopiesMessage" />
</h:inputText>

Notice the converterMessage attribute.

And while this might look like violating DRY (as you have a message in the entity already) it does not. Your model has nothing to do with a user submitting 'asd' as an integer - it is really a view matter

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top