Model form refresh - best practices?

For a newsletter plugin I show a ‘Send’ button in the controller update.php and a ‘Cancel Send’ button in a formwidget, depending on the newslettes state.

In my controller onQueue method and in my controller and also in the formwidget I do e.g.:

public function onCancelSend() {
        if (!isset($this->model->id)) {
            throw new \ApplicationException('An error occured. The newsletter data are not available');
        } else {
            $model = NewsletterModel::findOrFail($this->model->id);
            $model->send_queued = 0;
            $model->status = 'draft';
            $model->sent_date = null;
            $model->save();

            return \Redirect::refresh(); // best practice?
        }
    }

Is return \Redirect::refresh(); here the way to go?

First, I also just tried to add the attribute data-request-data="redirect:1" to my buttons, but this did not work.

Not sure on whether this is best practice or not, but the way I’ve done this in the past is by initialising the form again, and just refreshing the form field(s) that I want to update.

For example…

    /**
     * Update the contact field on the deal form
     *
     * @return string - Markup for the contact record finder field
     */
    public function onUpdateDealContactField()
    {
        // get the Deal model
        $deal = $this->getDealmodel();

        // Do some logic to update deal, dispatch a job or whatever....

        // Initialise the controller's form with the deal
        $this->initForm($deal);
        
        // Return the updated form field, in this case my 'contact' field
        return [
            '#Form-field-Deal-contact' => $this->formRenderField('contact')
        ];
    }
1 Like