Internal representation and user representation


It might often be necessary to display and edit in an different format as internally required. A simple example: Currency. Internally it’s a simple float-value, but the user might want to see “EUR 1.234,56” (note the currency and the changed decimal). And he might even enter values in such a format. The same goes for dates and so on.

Of course I am aware of the possibility to use beforeValidate and so on in order to convert data back and forth. But this approach has a major drawback: Since data has not been validated before, convertion might fail: Parts of the attributes will be in the native format and other parts in the user-format.

So i was thinking about having different representations for internal format and user format.

For a long time Delphi has been my major development tool. The definition of a Currency-Field definition contains properties like this:

DisplayFormat: Format while displying

EditFormat: Format while editing

So if I specify a format of ‘Hello World #,##0.00’ in DisplayFormat, the value will be represented as ‘Hello World 1,234.00’ in the user interface.

so my idea is to introduce new functions for properties in CModel. These functions are used to convert attributes back and forth:

getFmtAttribute(‘field’) might return ‘EUR 1.234.56’ instead of 1234.56

setFmtAttribute(‘field’, Value) sets the attribute based on the formatted string.

and for mass-assignment: setFmtAttributes and getFmtAttributes.

the model might also need a function attributeFormats() which returns a list of format-informations. may be like:


‘invoice_total’ => array(‘currency’, ‘fmt’ => ‘#,##0.00’),

‘invoice_date’ => array(‘time’)


In order not to break existing code, node conversion happens. If no additional ‘fmt’ information is specified, the formatting is based on CLocale. It’s great that yii already contains localization-information, but at the moment it does barely use it.

I think the implementation would be quite straight-forward. There is just one problem: Widgets and other View-Helper-functions would need to be changed to access the formatted value instead of the attribute itself.

What do you guys think about this idea?