Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7568247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:52:35+00:00 2026-05-30T14:52:35+00:00

I’m defining some bean validation messages for later i18n. This message should validate that

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:52:36+00:00Added an answer on May 30, 2026 at 2:52 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.