Form fields... Field that gathers Data from an external API, only load when displayed in UI


#1

Hello there,

I am programming an interface for another web-software using their API.
In one on my UIs, I want to provide a dropdown where entries from the API are listed.
But: I only want to load the Data from the API in this UI, so defining it in the model is not an option:

$this->addFields(['debitoor_id_payfull',   'type'=>'string',  'caption' => 'Debitoor ID Normalpreis', 'ui' => [ 'form' => ['DropDown', 'values' => (new \Pmg\Data\DebitoorProduct())->getProductArray()]]],
...

This loads the Data from the API each time the Model is loaded, obviously not a good Idea.

Aaaaah, just found an easy solution :slight_smile:

I only define in the model that the field should be DropDown, and add the values to the DropDown in the UI page.

Model (excerpt of addFields() function):

['debitoor_id_payfull',   'type'=>'string',  'caption' => 'Debitoor ID Normalpreis', 'ui' => [ 'form' => ['DropDown']]],
['debitoor_id_payreduced','type'=>'string',  'caption' => 'Debitoor ID reduzierter Preis', 'ui' => [ 'form' => ['DropDown']]],

UI page:

$values = (new \Pmg\Data\DebitoorProduct())->getProductArray();
$main_content->mainForm->fields['debitoor_id_payfull']->values = $values;
$main_content->mainForm->fields['debitoor_id_payreduced']->values = $values;

#2

just small note - form can’t exist without model. Your model does not need to have persistence but it’s required simpy because we didn’t wish to duplicate code in UI.


#3

Hi,
above model code was only an excerpt from model->addfields function :wink: