Save and preview

How can i code button save and preview in create or update backend form controller? I know that i can add onSavePreview() custom method in controller and code it by hand but maybe there is more simpliest way?

Hi @mkinternet ,

Just to confirm, are you looking to add a “Save and Preview” button that saves the form and then redirects the user to a preview of the record?

If so, you can use the following:

<button
    type="submit"
    data-request="onSave"
    data-request-data="redirect:1"
    data-hotkey="ctrl+s, cmd+s"
    data-load-indicator="<?= e(trans('backend::lang.form.saving')) ?>"
    class="btn btn-primary">
    Save and Preview
</button>

Don’t forget to set the redirection in config_form.yaml.

For instance, if this button is on a create.php page, add this to config_form.yaml:

# Create page
create:
    title: Create XYZ
    redirect: author/plugin/yourModelPlural/preview/:id

For an update form, you’ll do the same under the update: section.

1 Like

Button redirects to controller listing (index) not preview. Tested on OC 1 and OC 3

What’s defined in form controller behavior’s config? See redirectClose parameter: Form Controller - October CMS - 3.x