I have solved this problem this way.

1) Extend a component that you want to use for data entry, such as a TextInput component.

2) I add a private property with called _dataProvider OR _dateReference.

3) I then add a public getter ( with the [Bindable] tag if you want to allow others to bind to you) and setter for this property

4) In the setter is where you put your business logic related to how you want the string to appear in the text box. You set the text box 'text" property explicitly here. Because the setter will always get called when the bound data changes, you are essentially binding the text box to the value as well.

5) Add a valueCommitt, focusOut or change event handler to the component so that you can handle the input data. This handler will set your private variable when the user enters some input.

That should do it. Of course you can extend this further with formatters or switch statements to handle different feature... etc... but hopefully you get the idea. It is pretty simple and very powerful.

In your view code you will just write something like this.

 <MyCustomDateEntry id="myDate" dateReference="{model.someDate}" />

You can bind a Date object from the model, but the text box will show whatever you tell it to. To get the Date back you just use myDate.dateReference.

You may also want to check out the DateField control. It pretty much has the functionality you described built into it... with a nice calendar to boot.

- Kevin




On Dec 10, 2007, at 1:22 PM, Todd wrote:

Hello All,
I've been trying to wrap my mind around this.
I have a ValueBoject model that has a date properties and number
properties (one of many) that are bound to a TextInput fields on a form.
What's the best way to handle two-way data validating that the text
input has a valid date in it. More importantly, how do you bind a
specifically formatted date from the Model.
Say my model has a valid Date on it, and I want to have it displayed
in a textBox as 12/10/2007. The user changes the text box, and then I
want the model to have a valid Date object from the input text. The
key here is that the format input by a user and the format displayed
in the TextInput is different than the raw data that is bound to the
model.
What to do if I bind a new date that hasn't been set in the model
yet and I want something like a "-" (Hyphen) to appear in the form.
It seems like the default databinding functionality breaks down
pretty quickly. What are some of the techniques people use to convert
items on a VO object for display purposes in a form, take form input
and format it for specific model data?
Or, do I just need to not use default databinding of my model to
form inputs and instead have a method, that takes the VO, and manually
binds the data, doing the appropriate conversions as necessary. And
then to go the other way, after the form has been filled in and is
validated, covert to the model?

Thanks for any suggestions.




Reply via email to