Hi! How can I save changes made to a model record after clicking the save button in a custom modal form?
Custom save button in /controllers/news/update.php
<?= Form::open(['class' => 'layout']) ?>
<div class="layout-row">
<?= $this->formRender() ?>
</div>
<div class="form-buttons">
<div class="loading-indicator-container">
<button
type="submit"
data-handler="onOpenPopup"
data-control="popup"
data-size="large"
data-request-data="record_id: <?= $this->formGetModel()->id ?>"
data-load-indicator="<?= e(trans('backend::lang.form.saving')) ?>"
class="btn btn-primary">
<?= e(trans('backend::lang.form.save')) ?>
</button>
</div>
</div>
<?= Form::close() ?>
Popup partial controllers/news/popup.php
<?= Form::open(['data-request' => 'onSummarySave']) ?>
<input type="hidden" name="record_id" value="<?= e($record_id) ?>">
<div class="modal-header">
<h4 class="modal-title">Summary</h4>
<button type="button" class="btn-close" data-dismiss="popup"></button>
</div>
<div class="modal-body">
<?= $popupForm->render() ?>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Save</button>
<button type="button" class="btn btn-default" data-dismiss="popup">Cancel</button>
</div>
<?= Form::close() ?>
Functions in /controllers/News.php
public function onOpenPopup()
{
$record_id = post('record_id');
$this->prepareVarsLog();
return $this->makePartial('popup', [
'record_id' => $record_id
]);
}
protected function prepareVarsLog()
{
$model = new News;
$widgetConfig = $this->makeConfig('$/test/news/models/news/fields_popup.yaml');
$widgetConfig->model = $model;
$widgetConfig->alias = 'popupForm';
$this->vars['popupForm'] = $this->makeWidget('Backend\Widgets\Form', $widgetConfig);
}
public function onSummarySave()
{
$record_id = post('record_id');
$reason = post('edit_reason'); // field from fields_popup.yaml
$user_id = \BackendAuth::getUser()->id; // current user id
$news = News::find($record_id);
if ($news && $user_id) {
$logs = $news->changelog ?? [];
$newEntry = [
'updated_at' => \Carbon\Carbon::now()->toDateTimeString(),
'updated_by' => $user_id,
'update_reason' => $reason,
];
array_push($logs, $newEntry);
$news->changelog = $logs;
$news->save();
Flash::success('Saved');
} else {
Flash::error('Not saved');
}
}
The ‘edit_reason’ field is correctly saved as an entry in the changelog, but changes to e.g. the ‘Name’ or ‘Content’ fields in the main form are not updated when clicking the ‘Save’ button in the popup. How can I change this?