You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by George Christman <gc...@cardaddy.com> on 2013/02/12 21:56:45 UTC
Currency Converter
Hello, I'm wondering what the best solution would be to convert a string to
currency in Tapestry, but still use the tapestry validation. Example, if a
user enters $51,000.00, it would still be a valid amount, but the $ ,
characters would be stripped. From there it would run the custom Tapestry
Validation to ensure it's within range.
Things I thought of doing,
1) Provide the conversion in the getters and setters of the obj within the
page class,
2) creatie a Tapestry component that converts the values in the tml,
"value="t:currencyAmount(money)""
I'm just not sure how to intercept the money and convert it to a big decimal
without disrupting the validation. Does anybody have any suggestions?
hibernate entity
@Entity
public class BankMoney {
private BigDecimal money;
private BigDecimal getMoney() {
return money;
}
private void setBigDecimal(BigDecimal money) {
this.money = money;
}
Java
public class MyMoney {
@Property
private BankMoney bankMoney;
}
.tml
<t:TextField t:id="bankMoney.money"
validate="maxCurrency=99999999.99,minCurrency=-99999999.99"/>
--
View this message in context: http://tapestry.1045711.n5.nabble.com/Currency-Converter-tp5719990.html
Sent from the Tapestry - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: Currency Converter
Posted by Geoff Callender <ge...@gmail.com>.
That's a good observation. I'll try stripping off the currency symbol soon.
On 07/03/2013, at 1:44 AM, George Christman wrote:
> Nice Job! The only thing we might want to do is just strip the dollar sign
> on submit if someone adds it in rather than throw back the validation
> exception. Oddly enough, I find people doing it all the time. Everything
> else looks great!
>
> Thanks Geoff.
>
> On Tue, Mar 5, 2013 at 7:21 AM, Geoff Callender-2 [via Tapestry] <
> ml-node+s1045711n5720353h80@n5.nabble.com> wrote:
>
>> I've had a crack at improving it, and ended up dropping regex because it's
>> not well suited to multiple locales.
>>
>> I also dropped the currency symbol from the output because I found it made
>> parsing of negatives really difficult in some locales.
>>
>> See what you think:
>>
>>
>> http://jumpstart.doublenegative.com.au/jumpstart/examples/input/contributingtranslators
>>
>> http://jumpstart.doublenegative.com.au/jumpstart/fr/examples/input/contributingtranslators
>>
>> Geoff
>>
>> On 03/03/2013, at 10:50 PM, George Christman wrote:
>>
>>> Cool, I ended up converting my code to use regex too for the very same
>> reason. The one thing I did encounter was the removal of alpha from the
>> string rather than failing. Example, a1b2c3 would convert to 123 rather
>> than fail. I think I just through the validation exception if the string
>> contained anything other than $,.()-0-9. Thanks Geoff.
>>>
>>> -George
>>>
>>> ----- Reply message -----
>>> From: "Geoff Callender-2 [via Tapestry]" <[hidden email]<http://user/SendEmail.jtp?type=node&node=5720353&i=0>>
>>
>>> Date: Sun, Mar 3, 2013 1:54 am
>>> Subject: Currency Converter
>>> To: "George Christman" <[hidden email]<http://user/SendEmail.jtp?type=node&node=5720353&i=1>>
>>
>>>
>>>
>>>
>>>
>>>
>>> --
>>> View this message in context:
>> http://tapestry.1045711.n5.nabble.com/Currency-Converter-tp5719990p5720311.html
>>
>>> Sent from the Tapestry - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]<http://user/SendEmail.jtp?type=node&node=5720353&i=2>
>> For additional commands, e-mail: [hidden email]<http://user/SendEmail.jtp?type=node&node=5720353&i=3>
>>
>>
>>
>> ------------------------------
>> If you reply to this email, your message will be added to the discussion
>> below:
>>
>> http://tapestry.1045711.n5.nabble.com/Currency-Converter-tp5719990p5720353.html
>> To unsubscribe from Currency Converter, click here<http://tapestry.1045711.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=5719990&code=Z2NocmlzdG1hbkBjYXJkYWRkeS5jb218NTcxOTk5MHwxNjMyOTYxMjA3>
>> .
>> NAML<http://tapestry.1045711.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>
>
>
>
> --
> George Christman
> www.CarDaddy.com
> P.O. Box 735
> Johnstown, New York
>
>
>
>
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/Currency-Converter-tp5719990p5720371.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: Currency Converter
Posted by George Christman <gc...@cardaddy.com>.
Nice Job! The only thing we might want to do is just strip the dollar sign
on submit if someone adds it in rather than throw back the validation
exception. Oddly enough, I find people doing it all the time. Everything
else looks great!
Thanks Geoff.
On Tue, Mar 5, 2013 at 7:21 AM, Geoff Callender-2 [via Tapestry] <
ml-node+s1045711n5720353h80@n5.nabble.com> wrote:
> I've had a crack at improving it, and ended up dropping regex because it's
> not well suited to multiple locales.
>
> I also dropped the currency symbol from the output because I found it made
> parsing of negatives really difficult in some locales.
>
> See what you think:
>
>
> http://jumpstart.doublenegative.com.au/jumpstart/examples/input/contributingtranslators
>
> http://jumpstart.doublenegative.com.au/jumpstart/fr/examples/input/contributingtranslators
>
> Geoff
>
> On 03/03/2013, at 10:50 PM, George Christman wrote:
>
> > Cool, I ended up converting my code to use regex too for the very same
> reason. The one thing I did encounter was the removal of alpha from the
> string rather than failing. Example, a1b2c3 would convert to 123 rather
> than fail. I think I just through the validation exception if the string
> contained anything other than $,.()-0-9. Thanks Geoff.
> >
> > -George
> >
> > ----- Reply message -----
> > From: "Geoff Callender-2 [via Tapestry]" <[hidden email]<http://user/SendEmail.jtp?type=node&node=5720353&i=0>>
>
> > Date: Sun, Mar 3, 2013 1:54 am
> > Subject: Currency Converter
> > To: "George Christman" <[hidden email]<http://user/SendEmail.jtp?type=node&node=5720353&i=1>>
>
> >
> >
> >
> >
> >
> > --
> > View this message in context:
> http://tapestry.1045711.n5.nabble.com/Currency-Converter-tp5719990p5720311.html
>
> > Sent from the Tapestry - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]<http://user/SendEmail.jtp?type=node&node=5720353&i=2>
> For additional commands, e-mail: [hidden email]<http://user/SendEmail.jtp?type=node&node=5720353&i=3>
>
>
>
> ------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
>
> http://tapestry.1045711.n5.nabble.com/Currency-Converter-tp5719990p5720353.html
> To unsubscribe from Currency Converter, click here<http://tapestry.1045711.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=5719990&code=Z2NocmlzdG1hbkBjYXJkYWRkeS5jb218NTcxOTk5MHwxNjMyOTYxMjA3>
> .
> NAML<http://tapestry.1045711.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>
--
George Christman
www.CarDaddy.com
P.O. Box 735
Johnstown, New York
--
View this message in context: http://tapestry.1045711.n5.nabble.com/Currency-Converter-tp5719990p5720371.html
Sent from the Tapestry - User mailing list archive at Nabble.com.
Re: Currency Converter
Posted by Geoff Callender <ge...@gmail.com>.
I've had a crack at improving it, and ended up dropping regex because it's not well suited to multiple locales.
I also dropped the currency symbol from the output because I found it made parsing of negatives really difficult in some locales.
See what you think:
http://jumpstart.doublenegative.com.au/jumpstart/examples/input/contributingtranslators
http://jumpstart.doublenegative.com.au/jumpstart/fr/examples/input/contributingtranslators
Geoff
On 03/03/2013, at 10:50 PM, George Christman wrote:
> Cool, I ended up converting my code to use regex too for the very same reason. The one thing I did encounter was the removal of alpha from the string rather than failing. Example, a1b2c3 would convert to 123 rather than fail. I think I just through the validation exception if the string contained anything other than $,.()-0-9. Thanks Geoff.
>
> -George
>
> ----- Reply message -----
> From: "Geoff Callender-2 [via Tapestry]" <ml...@n5.nabble.com>
> Date: Sun, Mar 3, 2013 1:54 am
> Subject: Currency Converter
> To: "George Christman" <gc...@cardaddy.com>
>
>
>
>
>
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/Currency-Converter-tp5719990p5720311.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: Currency Converter
Posted by George Christman <gc...@cardaddy.com>.
Cool, I ended up converting my code to use regex too for the very same reason. The one thing I did encounter was the removal of alpha from the string rather than failing. Example, a1b2c3 would convert to 123 rather than fail. I think I just through the validation exception if the string contained anything other than $,.()-0-9. Thanks Geoff.
-George
----- Reply message -----
From: "Geoff Callender-2 [via Tapestry]" <ml...@n5.nabble.com>
Date: Sun, Mar 3, 2013 1:54 am
Subject: Currency Converter
To: "George Christman" <gc...@cardaddy.com>
--
View this message in context: http://tapestry.1045711.n5.nabble.com/Currency-Converter-tp5719990p5720311.html
Sent from the Tapestry - User mailing list archive at Nabble.com.
Re: Currency Converter
Posted by Geoff Callender <ge...@gmail.com>.
Done.
http://jumpstart.doublenegative.com.au/jumpstart/examples/input/contributingtranslators
Thanks again,
Geoff
On 14/02/2013, at 9:31 PM, Geoff Callender wrote:
> Thanks George. I'll look for a way to work that in.
>
> Geoff
>
> On 14/02/2013, at 2:49 AM, George Christman wrote:
>
>> Geoff, Thanks for the help. I was able to create a currency translator based
>> on your example, so feel free to use it with JumpStart to help others or
>> improve upon it.
>>
>> .properties
>>
>> currency-format-exception=You must provide a valid currency containing only
>> $,.0-9 for %s.
>>
>> .java
>>
>> import java.math.BigDecimal;
>> import java.text.NumberFormat;
>> import java.text.ParseException;
>> import org.apache.tapestry5.Field;
>> import org.apache.tapestry5.MarkupWriter;
>> import org.apache.tapestry5.Translator;
>> import org.apache.tapestry5.ValidationException;
>> import org.apache.tapestry5.services.FormSupport;
>>
>> /**
>> *
>> * @author gmc07
>> */
>> public class CurrencyTranslator implements Translator<BigDecimal> {
>>
>> private final String name;
>> private final Class<BigDecimal> type;
>> private final String messageKey;
>>
>> public CurrencyTranslator() {
>> this.name = "currency";
>> this.type = BigDecimal.class;
>> this.messageKey = "currency-format-exception";
>> }
>>
>> public String getName() {
>> return name;
>> }
>>
>> public String toClient(BigDecimal t) {
>> return NumberFormat.getCurrencyInstance().format(t);
>> }
>>
>> public Class<BigDecimal> getType() {
>> return type;
>> }
>>
>> public String getMessageKey() {
>> return messageKey;
>> }
>>
>> public BigDecimal parseClient(Field field, String clientValue, String
>> message) throws ValidationException {
>> if (clientValue == null) {
>> return null;
>> }
>>
>> NumberFormat nf = NumberFormat.getCurrencyInstance();
>> try {
>> return new BigDecimal(nf.parse(clientValue).toString());
>> } catch (ParseException ex) {
>> throw new ValidationException(message);
>> }
>> }
>>
>> public void render(Field field, String string, MarkupWriter writer,
>> FormSupport formSupport) {
>> // Do nothing; we don't yet support client-side validation.
>> // formSupport.addValidation(field, "currency", writer, null);
>> }
>> }
>>
>> .appModule
>> @SuppressWarnings("rawtypes")
>> public static void
>> contributeTranslatorAlternatesSource(MappedConfiguration<String, Translator>
>> configuration) {
>> configuration.add("currency", new CurrencyTranslator());
>> }
>>
>> .tml
>> <t:TextField t:id="price" t:translate="currency"/>
>>
>>
>>
>>
>> --
>> View this message in context: http://tapestry.1045711.n5.nabble.com/Currency-Converter-tp5719990p5720015.html
>> Sent from the Tapestry - User mailing list archive at Nabble.com.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: Currency Converter
Posted by Geoff Callender <ge...@gmail.com>.
Thanks George. I'll look for a way to work that in.
Geoff
On 14/02/2013, at 2:49 AM, George Christman wrote:
> Geoff, Thanks for the help. I was able to create a currency translator based
> on your example, so feel free to use it with JumpStart to help others or
> improve upon it.
>
> .properties
>
> currency-format-exception=You must provide a valid currency containing only
> $,.0-9 for %s.
>
> .java
>
> import java.math.BigDecimal;
> import java.text.NumberFormat;
> import java.text.ParseException;
> import org.apache.tapestry5.Field;
> import org.apache.tapestry5.MarkupWriter;
> import org.apache.tapestry5.Translator;
> import org.apache.tapestry5.ValidationException;
> import org.apache.tapestry5.services.FormSupport;
>
> /**
> *
> * @author gmc07
> */
> public class CurrencyTranslator implements Translator<BigDecimal> {
>
> private final String name;
> private final Class<BigDecimal> type;
> private final String messageKey;
>
> public CurrencyTranslator() {
> this.name = "currency";
> this.type = BigDecimal.class;
> this.messageKey = "currency-format-exception";
> }
>
> public String getName() {
> return name;
> }
>
> public String toClient(BigDecimal t) {
> return NumberFormat.getCurrencyInstance().format(t);
> }
>
> public Class<BigDecimal> getType() {
> return type;
> }
>
> public String getMessageKey() {
> return messageKey;
> }
>
> public BigDecimal parseClient(Field field, String clientValue, String
> message) throws ValidationException {
> if (clientValue == null) {
> return null;
> }
>
> NumberFormat nf = NumberFormat.getCurrencyInstance();
> try {
> return new BigDecimal(nf.parse(clientValue).toString());
> } catch (ParseException ex) {
> throw new ValidationException(message);
> }
> }
>
> public void render(Field field, String string, MarkupWriter writer,
> FormSupport formSupport) {
> // Do nothing; we don't yet support client-side validation.
> // formSupport.addValidation(field, "currency", writer, null);
> }
> }
>
> .appModule
> @SuppressWarnings("rawtypes")
> public static void
> contributeTranslatorAlternatesSource(MappedConfiguration<String, Translator>
> configuration) {
> configuration.add("currency", new CurrencyTranslator());
> }
>
> .tml
> <t:TextField t:id="price" t:translate="currency"/>
>
>
>
>
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/Currency-Converter-tp5719990p5720015.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: Currency Converter
Posted by George Christman <gc...@cardaddy.com>.
Do to some over looked situations such as negatives or values submitted
without a $ but with a comma, regular expressions are the better choice.
public BigDecimal parseClient(Field field, String clientValue, String
message) throws ValidationException {
if (clientValue == null) {
return null;
}
try {
clientValue = clientValue.replaceAll( "[^\\d\\-\\.]", "" );
return new BigDecimal(clientValue.toString());
} catch (NumberFormatException ex) {
throw new ValidationException(message);
}
}
--
View this message in context: http://tapestry.1045711.n5.nabble.com/Currency-Converter-tp5719990p5720019.html
Sent from the Tapestry - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: Currency Converter
Posted by George Christman <gc...@cardaddy.com>.
Geoff, Thanks for the help. I was able to create a currency translator based
on your example, so feel free to use it with JumpStart to help others or
improve upon it.
.properties
currency-format-exception=You must provide a valid currency containing only
$,.0-9 for %s.
.java
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParseException;
import org.apache.tapestry5.Field;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.Translator;
import org.apache.tapestry5.ValidationException;
import org.apache.tapestry5.services.FormSupport;
/**
*
* @author gmc07
*/
public class CurrencyTranslator implements Translator<BigDecimal> {
private final String name;
private final Class<BigDecimal> type;
private final String messageKey;
public CurrencyTranslator() {
this.name = "currency";
this.type = BigDecimal.class;
this.messageKey = "currency-format-exception";
}
public String getName() {
return name;
}
public String toClient(BigDecimal t) {
return NumberFormat.getCurrencyInstance().format(t);
}
public Class<BigDecimal> getType() {
return type;
}
public String getMessageKey() {
return messageKey;
}
public BigDecimal parseClient(Field field, String clientValue, String
message) throws ValidationException {
if (clientValue == null) {
return null;
}
NumberFormat nf = NumberFormat.getCurrencyInstance();
try {
return new BigDecimal(nf.parse(clientValue).toString());
} catch (ParseException ex) {
throw new ValidationException(message);
}
}
public void render(Field field, String string, MarkupWriter writer,
FormSupport formSupport) {
// Do nothing; we don't yet support client-side validation.
// formSupport.addValidation(field, "currency", writer, null);
}
}
.appModule
@SuppressWarnings("rawtypes")
public static void
contributeTranslatorAlternatesSource(MappedConfiguration<String, Translator>
configuration) {
configuration.add("currency", new CurrencyTranslator());
}
.tml
<t:TextField t:id="price" t:translate="currency"/>
--
View this message in context: http://tapestry.1045711.n5.nabble.com/Currency-Converter-tp5719990p5720015.html
Sent from the Tapestry - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: Currency Converter
Posted by Geoff Callender <ge...@gmail.com>.
I think a translator is exactly what you need. See if option 4 here helps:
http://jumpstart.doublenegative.com.au/jumpstart/examples/input/augmentingtranslators
On 13 February 2013 08:17, George Christman <gc...@cardaddy.com> wrote:
> Would a translator be the proper solution?
>
>
>
> --
> View this message in context:
> http://tapestry.1045711.n5.nabble.com/Currency-Converter-tp5719990p5719991.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>
Re: Currency Converter
Posted by George Christman <gc...@cardaddy.com>.
Would a translator be the proper solution?
--
View this message in context: http://tapestry.1045711.n5.nabble.com/Currency-Converter-tp5719990p5719991.html
Sent from the Tapestry - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org