Prepopulating a form with data from another table

Status
Not open for further replies.

rackem

Well-Known Member
I want to allow users to pre-populate a form with data from another table. I want to transfer a date, a textarea, and the file location of a fileupload.

I added a redirect in the form of table #1 that runs based on the selection of a dropdown. My jump page to form of table #2 is:

/index.php?option=com_fabrik&view=form&formid=67&mps_tournaments___date={mps_tournament_submit___event_date}&mps_tournaments___format={mps_tournament_submit___event_info}&mps_tournaments___flyer={mps_tournament_submit___flyer}

The textarea comes in as expected in the form of table #2. However, the date is off by one day and the file location is empty for the fileupload. I'm guessing there is a double timezone offset happening but not sure why the fileupload path isn't transferred? I tried with and without _raw.

I noticed that when I edit a form containing a fileupload, the fileupload is shown empty even if a file was added previously? There is however a file shown in detail view and the path is in my database table.
 
Do you have "Show media in form" set correctly? By default, we don't show the previously uploaded file.

You can't pre-populate an upload. That's not a Fabrik thing, that's an HTML security thing. Otherwise Bad People <tm> could do things like pre-populate the path with things like /etc/passwd, or the Windows equiv., and hide the upload element.

-- hugh
 
Well shucks, there goes that idea. Thanks for explaining. - makes sense why it isn't possible.

Would you recommend another approach? Perhaps I could perform the operation directly on the database using Form PHP? It would just be really nice to have a way to open up the form afterwards for edit as there are some additional fields the user would need to fill in.

Do you have "Show media in form" set correctly? By default, we don't show the previously uploaded file.
I don't want to see the file (these are images), just the file name. The "Show media in form" options don't seem to provide that.
 
The only way I've managed to do this in the past is to actually pre-create the second form, and redirect to the newly created form. I seem to recall I did it with a form submission script on the first form, which manually creates the row in the second form's table using the values I wanted to preset, and storing the newly created id in a hidden element on the first form (like form1___form2_rowid), and using that as a placeholder in a redirect ... index.php?option=com_fabrik&view=form&formid=123&rowid={form1___form2_id}. And in the submission plugin, I check to see if form2_rowid already has a value ... if it does, update that row in form2 instead of creating it.

Re the filename ... ah, yeah ... I think we must have screwed up when we converted the upload element to use layouts recently ... even if 'show media' is off, we still need to show the filename and a delete button. I'm fixing that now, I'll commit the fix later.

-- hugh
 
OK, I think I've fixed that issue with the file not showing when 'show media in form' is set to No.

-- hugh
 
OK, here is the code I used to make this work based on your suggestion and code I found in the wiki under the Form PHP plugin. :)

This goes in Form PHP, set to OnBeforeStore.
PHP:
$db = JFactory::getDbo();
$status = $data['mps_tournament_submit___status_raw'];
$e_date = $data['mps_tournament_submit___event_date_raw'];
$e_info = $data['mps_tournament_submit___event_info'];
$e_flyer = $data['mps_tournament_submit___flyer_raw'];

// Add back hours to date as these get removed for some reason above and it messes with date in new form
$e_date = date('Y-m-d H:i', strtotime('+6 hours',strtotime($e_date)));

if($status[0] == -1) {   
    $query = $db->getQuery(true);
    $query->insert('mps_tournaments')
        ->set('date = ' . $db->quote($e_date))
        ->set('description = ' . $db->quote($e_info))
        ->set('flyer = ' . $db->quote($e_flyer));
    $db->setQuery($query);
    $db->execute();
    $r_id = $db->insertid();
    // Update form data with insert id so a form redirect can open new entry
    $formModel->updateFormData('mps_tournament_submit___xfer_id', $r_id, true);
    $formModel->updateFormData('mps_tournament_submit___status', '1', true);
}

This is in a Form Redirect.
Jump page: index.php?option=com_fabrik&view=form&formid=67&rowid={mps_tournament_submit___xfer_id}
Condition: return '{mps_tournament_submit___xfer_id}' > 0;

OK, I think I've fixed that issue with the file not showing when 'show media in form' is set to No.
Fix confirmed.

Thank you!
 
That looks perfect.

BTW, the "date messing up" thing is because (as I'm sure you know) we store dates in the database as UTC, unless you set "store as local" on the date element. Fabrik then handles removing the TZ offset when writing out form data, and applying it when displaying.

So when you store dates "by hand" like this, in a table Fabrik will be using for dates in "Store as UTC", you have to make sure you store them as UTC, with the TZ offset removed.

-- hugh
 
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top