Re: Locale-aware decimal number validation

2009-01-11 Thread Joakim Olsson
Aaah...Great. Didn't even consider searching the JIRA before diving into
the tapestry-code. :-D

I attached my patch to the JIRA as well.

Regards,
Joakim


On Sun, 2009-01-11 at 14:36 +0100, Ulrich Stärk wrote:
> Joakim,
> 
> there has been quite some discussion on this already and there also 
> exists a JIRA issue for this. Maybe you want to contribute there: 
> https://issues.apache.org/jira/browse/TAP5-211 Please also sign up for 
> JIRA and vote for it.
> 
> Cheers,
> 
> Uli
> 
> Joakim Olsson schrieb:
> > Hi,
> > 
> > I have a need for locale-aware number validation in my app (since Sweden
> > use comma as decimal-separator instead of dot) and therefor I created my
> > own DoubleTranslator and FloatTranslator that I contributed to the
> > TranslatorSource from my module.
> > 
> > Since it would have been nice to also have client validation working in
> > a locale-aware manner I decided to take it a step further. :-D
> > 
> > Please find attached a patch towards 5.0.19-SNAPSHOT which adds support
> > for both client- and server-side locale-aware number-validation.
> > 
> > I have the number of decimals fixed to two in the translators since I
> > need to use it for monetary input. This should be nice to be able to
> > configure per field in some way.
> > 
> > I have placed the regexp for the client-side validation in the
> > ValidationMessages.properties-file which might be the wrong place to
> > have it.
> > 
> > There is probably also a need to look at the regular expressions that
> > are currently setup to make sure that they are correct for the various
> > locales.
> > 
> > Best regards,
> > Joakim
> > 
> > 
> > 
> > 
> > 
> > -
> > To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
> > For additional commands, e-mail: users-h...@tapestry.apache.org
> 
> 
> -
> To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
> For additional commands, e-mail: users-h...@tapestry.apache.org
> 
> 


-
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org



Re: Locale-aware decimal number validation

2009-01-11 Thread Ulrich Stärk

Joakim,

there has been quite some discussion on this already and there also 
exists a JIRA issue for this. Maybe you want to contribute there: 
https://issues.apache.org/jira/browse/TAP5-211 Please also sign up for 
JIRA and vote for it.


Cheers,

Uli

Joakim Olsson schrieb:

Hi,

I have a need for locale-aware number validation in my app (since Sweden
use comma as decimal-separator instead of dot) and therefor I created my
own DoubleTranslator and FloatTranslator that I contributed to the
TranslatorSource from my module.

Since it would have been nice to also have client validation working in
a locale-aware manner I decided to take it a step further. :-D

Please find attached a patch towards 5.0.19-SNAPSHOT which adds support
for both client- and server-side locale-aware number-validation.

I have the number of decimals fixed to two in the translators since I
need to use it for monetary input. This should be nice to be able to
configure per field in some way.

I have placed the regexp for the client-side validation in the
ValidationMessages.properties-file which might be the wrong place to
have it.

There is probably also a need to look at the regular expressions that
are currently setup to make sure that they are correct for the various
locales.

Best regards,
Joakim





-
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org



-
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org



Locale-aware decimal number validation

2009-01-11 Thread Joakim Olsson
Hi,

I have a need for locale-aware number validation in my app (since Sweden
use comma as decimal-separator instead of dot) and therefor I created my
own DoubleTranslator and FloatTranslator that I contributed to the
TranslatorSource from my module.

Since it would have been nice to also have client validation working in
a locale-aware manner I decided to take it a step further. :-D

Please find attached a patch towards 5.0.19-SNAPSHOT which adds support
for both client- and server-side locale-aware number-validation.

I have the number of decimals fixed to two in the translators since I
need to use it for monetary input. This should be nice to be able to
configure per field in some way.

I have placed the regexp for the client-side validation in the
ValidationMessages.properties-file which might be the wrong place to
have it.

There is probably also a need to look at the regular expressions that
are currently setup to make sure that they are correct for the various
locales.

Best regards,
Joakim

Index: tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ClientBehaviorSupportImpl.java
===
--- tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ClientBehaviorSupportImpl.java	(revision 733115)
+++ tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ClientBehaviorSupportImpl.java	(working copy)
@@ -18,9 +18,12 @@
 import org.apache.tapestry5.Link;
 import org.apache.tapestry5.RenderSupport;
 import org.apache.tapestry5.corelib.data.InsertPosition;
+import org.apache.tapestry5.ioc.Messages;
 import org.apache.tapestry5.ioc.internal.util.Defense;
 import org.apache.tapestry5.json.JSONArray;
 import org.apache.tapestry5.json.JSONObject;
+import org.apache.tapestry5.services.PersistentLocale;
+import org.apache.tapestry5.services.ValidationMessagesSource;
 
 public class ClientBehaviorSupportImpl implements ClientBehaviorSupport
 {
@@ -28,9 +31,16 @@
 
 private final JSONObject validations = new JSONObject();
 
-public ClientBehaviorSupportImpl(RenderSupport renderSupport)
+private final ValidationMessagesSource validationMessagesSource;
+
+private final PersistentLocale locale;
+
+public ClientBehaviorSupportImpl(RenderSupport renderSupport, ValidationMessagesSource validationMessagesSource, PersistentLocale locale)
 {
 this.renderSupport = renderSupport;
+this.validationMessagesSource = validationMessagesSource;
+this.locale = locale;
+
 }
 
 public void addZone(String clientId, String showFunctionName, String updateFunctionName)
@@ -141,5 +151,6 @@
 
 renderSupport.addInit("validate", parameters);
 }
-}
+Messages messages = validationMessagesSource.getValidationMessages(locale.get());
+renderSupport.addInit("localeSpecificFloatRegexp", messages.get("floatRegexp"));}
 }
Index: tapestry-core/src/main/java/org/apache/tapestry5/internal/translator/FloatTranslator.java
===
--- tapestry-core/src/main/java/org/apache/tapestry5/internal/translator/FloatTranslator.java	(revision 733115)
+++ tapestry-core/src/main/java/org/apache/tapestry5/internal/translator/FloatTranslator.java	(working copy)
@@ -14,29 +14,40 @@
 
 package org.apache.tapestry5.internal.translator;
 
+import java.text.NumberFormat;
+import java.text.ParseException;
+
 import org.apache.tapestry5.Field;
 import org.apache.tapestry5.ValidationException;
+import org.apache.tapestry5.services.PersistentLocale;
 
 public class FloatTranslator extends DecimalNumberTranslator
 {
-public FloatTranslator()
+private final PersistentLocale locale;
+
+public FloatTranslator(PersistentLocale locale)
 {
 super("float", Float.class);
+this.locale = locale;
 }
 
 public String toClient(Float value)
 {
-return value.toString();
+NumberFormat formatter = NumberFormat.getNumberInstance(this.locale.get());
+formatter.setMinimumFractionDigits(2);
+formatter.setMaximumFractionDigits(2);
+return formatter.format(value);
 }
 
-public Float parseClient(Field field, String clientValue, String message)
-throws ValidationException
+public Float parseClient(Field field, String clientValue, String message) throws ValidationException
 {
+NumberFormat formatter = NumberFormat.getNumberInstance(this.locale.get());
+
 try
 {
-return new Float(clientValue.trim());
+return formatter.parse(clientValue.trim()).floatValue();
 }
-catch (NumberFormatException ex)
+catch (ParseException ex)
 {
 throw new ValidationException(message);
 }
Index: tapestry-core/src/main/java/org/apache/tapestry5/internal/translator/DoubleTranslator.java
===
--- tape