Re: DB_DataObject_FormBuilder : 1:many relation
| From: | Justin Patrin | Date: | Tue, 19 Apr 2005 18:14:33 +0000 |
| Subject: | Re: DB_DataObject_FormBuilder : 1:many relation | ||
| References: | 1 2 3 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-18801@lists.php.net to get a copy of this message | ||
On 4/19/05, Thomas Balthazar <tba@voxteneo.com> wrote:
> Hi Justin,
>
> I've downloaded your SubForm class at
> <http://fb.reversefold.com/SubForm.phps>.
> It's easy to create subForm with it, but how do you set up a link which
> hides / displays the subForm?
> Also, is it possible, with your subForm to add subForm "on demand".
> For instance, a DOCUMENT can have many AUTHORS, so I would like to have
> a DOCUMENT form with a link "add an author" which adds as many AUTHOR
> as we want.
This is all a bit out of scope for PEAR, but I'll give you some
pointer. You'll need JavaScript and CSS is very handy.
Add a hidden field which holds when the sub form is displayed.
$form->addElement('hidden', 'subFormDisplayed');
Use this CSS class:
.hidden {
overflow: hidden;
visibility: hidden;
display: none;
}
Apply that class to a div surrounding the SubForm (I use an eltered
elementTemplate for QF). You also need a link which calls the
javascript below (I've added it to the template for simplicity). Also,
only hide it if the sub form was not displayed (if the validation
fails you need to redisplay the form).
$renderer =& HTML_QuickForm::defaultRenderer();
$renderer->setElementTemplate(str_replace('{element}',
'<a
href="javascript:void();" onclick="showSubFormElement()">Show Sub
Form</a>
<div class="'.($_REQUEST['subFormDisplayed'] ? '' :
'hidden')."
id="idForElementDiv">{element}</div>',
$renderer->_elementTemplate),
'subFormElement');
Add the JavaScript with the function somewhere in your code.
<script language="javascript">
function newCorrectiveAction() {
if(document.getElementById("idForElementDiv").className == "hidden") {
document.getElementById("idForElementDiv").className = "";
document.getElementById("subFormDisplayed").value = "1";
} else {
document.getElementById("idForElementDiv").className = "hidden";
document.getElementById("subFormDisplayed").value = "0";
}
}
</script>
Now add the sub form element and also make sure to set up the
conditional validation. (assuming $subForm is your completed sub form.
Note that there may be reference problems here, it's best to have the
sub form finished before creating the element).
require_once('HTML/QuickForm/SubForm.php');
function subFormDisplayed($values) {
return $values['subFormDisplayed'] == 1;
}
$el =& HTML_QuickForm::createElement('subForm', 'subFormElement',
'', $subForm);
$el->setPreValidationCallback('subFormDisplayed');
Now it should all work. As for making *multiple* sub forms on one
page, that's much harder. I *can* do it, but I haven't yet and I don't
have th etime now. Let me know if you get it done. :-)
>
> Thanks for your help.
>
> Thomas.
>
> > 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.
>
> On 14 Apr 2005, at 19:39, Justin Patrin wrote:
>
> > 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
> >
> > --
> > PEAR General Mailing List (http://pear.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
Justin Patrin