$this->form->fill clears whole form

Hi folks, I have an action setup that when I click it, it will fetch data from an api. I don't want the data that has been fetched to update automatically. I use the following:
$this->form->fill([
'page_views' => $Response['pageviews'],
'unique_visits' => $Response['visitors'],
'avg_time_on_page' => $Response['visit_duration'],
'top_pages' => $this->topPages,
]);
$this->form->fill([
'page_views' => $Response['pageviews'],
'unique_visits' => $Response['visitors'],
'avg_time_on_page' => $Response['visit_duration'],
'top_pages' => $this->topPages,
]);
the above code is contained within the function that is called by my action/button. I have other input fields throughout the page that already have data outwith the page_views, unique_visits, avg_time_on_page and top_pages. When I click the action button and it successfully fills in the data fetched into the specified input fields, however, it clears all other input fields throughout the page that haven't been specified in the form->fill. How do i prevent this from happening? I also tried the following that was suggested online but had no luck:
$this->fillForm();
$this->fillForm();
3 Replies
LeandroFerreira
use $this->data['your_field'] = 'xx'
nathan269_
nathan269_OP3w ago
Thanks Leandro, so do i need to specify every single input field in the form? So if I had 15 input fields, I'd need to do the following?
$this->data['your_field'] = 'xx'
$this->data['your_field'] = 'xx'
$this->data['your_field'] = 'xx'
...
$this->data['your_field'] = 'xx'
$this->data['your_field'] = 'xx'
$this->data['your_field'] = 'xx'
...
LeandroFerreira
$fields = [
'field_1' => 'xx',
'field_2' => 'xx',
];

$this->data = array_merge($this->data, $fields);
$fields = [
'field_1' => 'xx',
'field_2' => 'xx',
];

$this->data = array_merge($this->data, $fields);
?

Did you find this page helpful?