Re: Boolean datatype in DB_DataObject & DB_DataObject_FormBuilder

From: Date: Wed, 17 Dec 2003 05:10:19 +0000
Subject: Re: Boolean datatype in DB_DataObject & DB_DataObject_FormBuilder
References: 1 2  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-24478@lists.php.net to get a copy of this message
Here is my solution/workaround for checkboxes with DataObject/FormBuilder, maybe you can do something with it (find a better solution to apply to FormBuilder, put this code into the documentation or do nothing with it at all): The value 0/1 is stored in the corresponding fields. preGenerateForm generates the checkboxes, and preProcess forces field values to be set to zero if they aren't equal to 1. (without this you couldn't uncheck checked checkboxes). All these code comes into your DataObject-derived classes. In this example 'mycheckbox' is an integer field (tinyint(1) in mysql). var $fieldLabels = array(
    'mycheckbox' => 'field label preceding the checkbox'
); var $booleanFields = array(
    'mycheckbox' => 'long description following the checkbox'
); function preGenerateForm() {
    $this->preDefElements = array();
    if (is_array($this->booleanFields)) {
        foreach ($this->booleanFields as $field=>$description) {
            $temp = &HTML_QuickForm::createElement('checkbox', $field, $this->fieldLabels[$field], $description);
            $temp->setChecked($this->$field);
            $this->preDefElements[$field] = &$temp;
        }
    }
} function preProcess(&$values) {
    if (is_array($this->booleanFields)) {
        foreach (array_keys($this->booleanFields) as $field) { 
            if ($values[$field] != 1) {
                $values[$field] = 0;
            }
        }
    }
} Regards, Norbert

« previous php.pear.dev (#24478) next »