Reputation: 35951
I've a Form where an property of source Item need to be formatter by an custom format.
Source property (of my own bean) is a Integer
, but need to be formatted as a Currency-like format.
I tried to implement my own PropertyFormatter
, and setup it inside my FieldFactory.createField
for this form as
TextField tf = new TextField("Price");
tf.setPropertyDataSource(new MyPriceFormatter());
return tf;
But as I see from the logs, only format()
method is called. But parse()
method is never used, and setValue
is never called
What's wrong with my code? How to use custom PropertyFomatter for forms? Or how to add custom format for form's field?
After some investigation i found that there is something just replaces my formatter, with an new MethodProperty
data source. So i'd implemented my own PriceField
, with overrided setPropertyDataSource
, that fix this situation. btw, it seems to bee hacky, and i'm still looking for an other way
Upvotes: 1
Views: 3732
Reputation: 5709
I have also experienced this problem and solved it another way. Actually I also had to make a textfield formated with a currency :-)
The problem is that the datasource in the PropertyFormatter is null at the time you are creating the fields in the FormFieldFactory. You can instead set the datasource on your field after the FormFieldFactory has been called:
addCountryRatesForm.setFormFieldFactory(new MyFormFieldFactory());
Field internationalRate = addCountryRatesForm.getField("internationalRate");
internationalRate.setPropertyDataSource(new CurrencyFormatter("#0.00 ", currency, internationalRate.getPropertyDataSource()));
So unfortunately with Vaadin you cannot create a TextField that sets its own formatter.
Upvotes: 1