Forum
Custom app: How do I auto-fill a form with item details from the database
I am trying to build a custom app on top of the Members App. Registered members will be able to manage contracts with said app. They log in, go to the contracts page and see the list of contracts, they have already put in.
Now, when they want to edit a contract they are taken to a form.
<perch:form id="editContract" method="post" app="frwssr_contracts">
<div id="vertragspartner">
<perch:label for="contractissuer">Vertragspartner</perch:label>
<perch:input class="text_input" type="text" id="contractissuer" disabled="disabled">
</div>
<perch:input type="submit" name="speichern" value="Daten speichern" id="btn_vertrag_speichern" class="btn_submit">
<perch:input type="hidden" id="token" />
<div id="btn_vertrag_abbrechen" class="btn_submit"><a href="?s=<perch:contracts id="contractID" />">abbrechen</a></div>
</perch:form>
How do I get this form to auto-fill with the contract details from the database?
"contractissuer" is the field alias for the database field "contractIssuer".
I tried to replicate the functionality from the Members App, but seem to be missing the magic detail, that accomplishes what I want.
Here's my code for calling the contact details:
function frwssr_contracts_details($memberID, $contractID, $opts=array(), $return=false)
{
$default_opts = array(
'template' => 'details.html',
'skip-template' => false,
);
if (is_array($opts)) {
$opts = array_merge($default_opts, $opts);
}else{
$opts = $default_opts;
}
$API = new PerchAPI(1.0, 'frwssr_contracts');
$Contracts = new Frwssr_Contracts($API);
$Contract = $Contracts->findContractDetails($memberID, $contractID);
$Template = $API->get('Template');
$Template->set(PerchUtil::file_path('contracts/'.$opts['template']), 'contracts');
$r = $Template->render_group($Contract);
$r = $Template->apply_runtime_post_processing($r, $Contract);
if ($return) return $r;
echo $r;
return false;
}
I could call <perch:contracts id="contractIssuer" /> as the value of the "contractissuer" field, but that is not, how it is done in the Members App.
What am I missing?
(Sorry, if this sounds confusing. I am somewhat struggling to make my issue clear.)
$Template->render_group
is rendering the template, so$Contract
should be the details you're supplying to the template.Thanks for the quick answer, Drew. (As always. :)
I guess, that is exactly where I am stuck. $Contract contains (among others) the object [details:protected], which holds the details I want to be displayed. I can't seem to figure out how to feed that object into an array.
Do you have a hint there?
Is it just one item or multiple?
render_group()
expects multiple items. Userender()
if it's just one.Also make sure you have a
to_array()
method to transform your object to a template-ready array.The structure of the $Contract object looks like this:
I tried to get a hold of the details with the following code:
But when I try to
print_r($Contract->to_array());
the only thing I get is the error message "Fatal error: Call to a member function to_array() on a non-object in /XXX/perch/addons/apps/frwssr_contracts/runtime.php on line 96".(Sorry for maybe asking for obvious answers, Drew. I am relatively new to object-oriented PHP.)
It's telling you that
$Contract
is not an object. So you need to look and see where's that's being created and trace back to why it's failing.$Contracts is filled like this:
and the corresponding function looked like this:
return_instances
seems to have created the non-object.I now changed the function to read
and thus get an array returned.
Switching
render_group($Contract)
torender($Contract)
then did the trick to fill the form.Thanks, Drew. You're the best—and the most patient, I suppose. ;)
(Which of your answers should I mark as solution, as all of them led to solving the problem. :D)
Sorry to open this up again. I thought, it would be better for the context, than to open a new thread.
Auto-filling the form worked great after Drew's help.
Now, how do I get the form to save?
Here's my function (details.html being the perch form template):
And here is what debug outputs after submission of the form:
After hitting submit the page gets reloaded with the edited input values. But the changes are not saved in the database and no success message appears (well, obviously as there is no success saving).
I would check out the Example app for an example of how to do this. You need to read the fields in and then pass them to either the
create()
orupdate()
method in order to commit the change.Thanks, Drew.
I had done just that, but got stuck anyway.
After another day of trying to track down my mistake I have been successful and the form is now working as desired.