Re: DB_DataObject_FormBuilder : 1:many relation
| From: | Justin Patrin | Date: | Thu, 14 Apr 2005 17:39:59 +0000 |
| Subject: | Re: DB_DataObject_FormBuilder : 1:many relation | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-18756@lists.php.net to get a copy of this message | ||
On 4/14/05, Thomas Balthazar <tba@voxteneo.com> wrote:
> Hi,
>
> I would like to manage a 1 to many relationship in one single form.
>
> Explanations :
> I have a DOCUMENTS table and a AUTHORS tables.
> A document can have multiple authors associated to it.
> An author can only be associated with one document.
> The DOCUMENTS primary key is DOC_ID (identity).
> AUTHORS has a foreign key to DOC_ID.
>
> I would like to be able to generate a form that allows me to fill in
> the values for a document and for the author at the same time . For
> instance :
> document title : ___
> document description : ___
> author name : ___
>
> How can I generate such a form?
> I tried to use Nested forms, but I cannot figure out how I can tell the
> AUTHORS.DOC_ID field to be set to the newly inserted DOCUMENT.DOC_ID
> value.
>
> Am I clear? :o)
>
> Thanks in advance for your help.
>
Funny you should mention this now, this is exactly what I created my
SubForm element to do. However, you can do it without the SubForm.
1) Create the Document form.
2) Create the Author form
2a) Use $fb->useForm() and pass in the Document form so that the
author form will have the document form at the top
2b) Use elementNamePrefix to make sure that the field names don't clash
3) If form validates (is submitted) then run $form->process() on both
of the formbuilder objects (once for the document and once for the
author).
3a) Before processing for the author, set the document id in the
dataobject you used to create the form (yes, it should still be the
same object in the formbuilder ;-) )
To do this with a SubForm, do the same as above but don't use
useForm(). Instead, add en element to the document form which is of
type subForm and pass in the author form. You may also want to make
use of setPreValidationCallback() on the subform to make it not
validate of nothing was entered.
I also set up, in my form, a link which hides / displays the subForm
and also sets a hidden value to '1' if the subForm is displayed so
that the script knows to validate and enter a new record. I also set
up a static element which displays a Structures_DataGrid filled out
with all of the previously entered sub-records.
--
Justin Patrin