PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Database » DB_DataObject_FormBuilder » Bug #8544

Setting elementTypeMap['datetime'] = 'text' is broken

Details

Submitted2006-08-23 17:38 UTC
Fromdtlmhn at dtl dot net
Assignedjustinpatrin
StatusWont fix
PackageDB_DataObject_FormBuilder
PHP VersionIrrelevant
OSIrrelevant
Roadmaps(Not assigned)

Comments

[2006-08-23 17:38 UTC] dtlmhn at dtl dot net

Description:
------------
Setting:

elementTypeMap['datetime'] = 'text'

correctly causes datetime fields to be rendered as text elements, but the initial value of the field is blank instead of the actual contents of the field.

This is true for 'date' fields as well, and I would imagine also 'time' fields.

The cause is that FormBuilder apparently has a default dateFromDatabaseCallback function that returns an array suitable for use in passing to HTML_QuickForm's setDefaults method (and, I presume, HTML_QuickForm_group::setValue). This is done in FormBuilder::_generateForm(): The callback is called in:

case (($type & DB_DATAOBJECT_DATE) && ($type & DB_DATAOBJECT_TIME)):

The return value is put into the $formValues assoc. array. At the end of _generateForm(), $formValues is (indirectly) passed to $this->_form->_setFormDefaults(), which then calls
QuickForm::setDefaults().

Thus, QuickForm is getting an array to setValue on a text element, which doesn't work -- HTML::Common tries to do htmlspecialchars() on the value, which causes a PHP warning:

[23-Aug-2006 10:11:03] PHP Warning: htmlspecialchars() expects parameter 1 to be string, array given in /usr/local/share/pear/HTML/Common.php on line 150

And, obviously, the desired value is never set.

A work-around is to setup your own dateFromDatabaseCallback method (which is probably desirable anyway since no user wants to look at a MySQL timestamp), but there should be some intelligent way of making this do the right thing (which would be to just set the value of the field to the contents of the field, directly).

Test script:
---------------
I'll give a description of a test scenario rather than a script:

1) setup a DB_DataObject table with a datetime field
2) in the DB_DataObject extended class set fb_elementTypeMap['datetime'] = 'text'
3) Set your datetime field's value to a valid formatted MySQL timestamp ($dbdo->setmydatetime = '2006-01-01 12:00:00')
4) Render a form for this data object via formbuilder

Expected result:
----------------
The form should render with a text field containing the timestamp.

Actual result:
--------------
Instead it renders with an empty text field.

SIDE-EFFECT: DB_DataObject_FormBuilder_QuickForm::_createDateTimeElement() does not check the mapped element type either, and as a result, it supplies what it believes to be the quickform 'date' element 'options' (3rd argument to the QuickForm_Element_date constructor) to the 3rd argument of whatever constructor happens to be called. In the case of 'text', this happens to be the atttributes argument, which causes the date options to be supplied as HTML INPUT element attributes:

<input format="M d Y h:i A" language="en" name="report_sent" type="text" size="24" maxlength="22" value="" id="report_sent" />