How can I correctly validate input of decimal numbers with comma decimal separator (eg. 12,34 ?)

863 views
Skip to first unread message

Tex

unread,
Oct 26, 2011, 5:27:30 PM10/26/11
to play-fr...@googlegroups.com
Hi,

I cannot understand how can I correctly validate input of decimal numbers with comma decimal separator (eg. 12,34)...

Assuming I have a very simple model:

@Entity
class Product {
  public String name;
  public float price;
}

and a controller action in which I save the object:

public static void save(@Valid Product product) {
  if (validation.hasErrors()) {
    params.flash();
    validation.keep();
    editProduct(product);
  } else {
    product.save();
  }
}

When play validates the object for price with comma decimal delimiter "12,34" it correctly rejects it because it is not a valid number, right, but I want accept the value with comma decimal delimiter...

How can I do ?

I know that in France the decimal separator is comma (like in Italy), is there anyone that has solved the problem and can show me a code example ? I cannot found any working solution at the moment...

Please help me..., many thanks in advance...

Ahmed Mahmoud

unread,
Oct 26, 2011, 5:29:48 PM10/26/11
to play-fr...@googlegroups.com
can't u handle this on submission?? I mean when the user submits the form u change the ',' into '.' ?

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/jFvk_GipsHYJ.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.



--

Ahmed Mahmoud Mohammed Abd El-wahab 
el shaheer b Ahmed Fat7y.

Tex

unread,
Oct 27, 2011, 2:27:09 AM10/27/11
to play-fr...@googlegroups.com
Thanks Ahmed for your response.

Yes I can change the value using javascript/jquery but I want to know if there is a cleaner/serverside solution because I have some other clients that executes the post (example: curl, RCP client, etc.) and I want to accept the numbers with dot or comma decimal separators...

Is there any way to do this ?

Ahmed Mahmoud

unread,
Oct 27, 2011, 2:36:25 AM10/27/11
to play-fr...@googlegroups.com
yes u're right =\ besides javascript can be disabled by some users..

No for sorry I donnu any clean way to do so directly.

but you can get around it like that

public static void save(String price, String name) {
.
.
.

if ( !(new Product(name, parsedPrice).validateAndSave()) )
{
    params.flash();
    validation.keep();
}

}

andinside the method you can replace the ',' with '.' and parse it =\

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/QyBrfGsyrgkJ.

To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

Tex

unread,
Oct 27, 2011, 2:42:31 AM10/27/11
to play-fr...@googlegroups.com
Many thanks Ahmed,

Now I try your solution, many thanks...

Ahmed Mahmoud

unread,
Oct 27, 2011, 2:45:38 AM10/27/11
to play-fr...@googlegroups.com
u r so welcome.
I hope it works .. 

On Thu, Oct 27, 2011 at 8:42 AM, Tex <giate...@gmail.com> wrote:
Many thanks Ahmed,

Now I try your solution, many thanks...

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/oBAnP0py4y8J.

To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

christian sarnataro

unread,
Oct 27, 2011, 6:02:39 AM10/27/11
to play-fr...@googlegroups.com
Hi,
this is just a suggestion, I didn't try it by myself. But if it works, it can be much a cleaner solution.

In play you can build your own binders, which "translate" an http string parameters in any type you want (in your case, a string with a comma in a decimal value).


What I'm not sure about is: if after binding a value with your own custom binder, the validation process take place as usual.

Please let me know if it works.

Richard North

unread,
Oct 27, 2011, 7:19:52 AM10/27/11
to play-fr...@googlegroups.com
Just a slight aside - It's not safe to store prices in float/double data types (see http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency) for some explanations of why.

Unfortunately it looks like the Play binding for BigDecimals (play.data.binding.Binder:609) doesn't use a localised DecimalFormat - otherwise you wouldn't have to worry about comma decimal separators either, because the string->BigDecimal conversion would take care of that for you. Maybe this could be an easy patch, though...

HTH
Richard

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/jFvk_GipsHYJ.

Tex

unread,
Oct 27, 2011, 10:54:30 AM10/27/11
to play-fr...@googlegroups.com
Many thanks Christian & Richard...

I've just refactor the application, now it uses BigDecimal for floating points (working for 4 years in ruby I've forgotten this rule...).

I think that the problem for decimal numbers with comma is not a "binding" problem but a "validation" problem, am I right ?

Johan Vosloo

unread,
Oct 27, 2011, 11:13:14 AM10/27/11
to play-fr...@googlegroups.com
I use a custom global binder for the same reason.
The binder strips out the comma, e.g. turns "1,000,000.99" into "1000000.99".
Binding and validation then works fine

Tex

unread,
Oct 27, 2011, 11:52:42 AM10/27/11
to play-fr...@googlegroups.com
MANY THANKS JOHAN !!!

The problem is solved, it works like a charm !

Many thanks to all for the great support !!!
Reply all
Reply to author
Forward
0 new messages