Re: FormBuilder Question

From: Date: Thu, 18 Mar 2004 16:27:18 +0000
Subject: Re: FormBuilder Question
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-11365@lists.php.net to get a copy of this message
David Thunman schrieb:
Hi, A quick question that bothers me: When I submit a form and save a new item, I want either that the form displays an empty form or that the newly created id is loaded into the form object. I; enable hidden fields with primary key hide_primary_key = 1 Is set and works fine when I call the page with a specific id but not after a newly saved item, do I have to add the value myself or shouldnt formbuilder do that for me?
My guess is that your call to processform looks something like this: $form->process(array($formBuilder, 'processform'), true); In this case, the FormBuilder object is being passed by value, meaning, PHP creates a *copy* of the object that is independent from the original one, which is not helpful in this case. Make sure you always pass all objects around by reference - this way, all data manipulations will always be reflected in the original object. To achieve that, you need to place an '&' in front of the variable. Typical usage of FormBuilder with references: $do =& Db_DataObject::factory('myTable'); $fb =& DB_DataObject_FormBuilder::create($do); $form =& $fb->getForm(); if ($form->validated()) {
    echo 'Until now, the ID was: '.$do->someField;
    $form->process(array(&$fb, 'processform'), true);
    echo '<br>The new ID is: '.$do->someField;
} If the ID shows up in the echo statement, it should also be in the form's hidden field because everything was correctly passed by reference, so every object is always aware of the current state of all related objects. Some more explanations: The create() method does not need the '&', because it is already defined in the method declaration that the parameter is expected to be passed by reference. The process method, however, needs to have the '&' before the first array element - otherwise PHP will make a copy here... I know this is very confusing, especially regarding that call time pass-by-reference is considered to be deprecated, but this is an exception... :-/ CU Markus

« previous php.pear.general (#11365) next »