在前端使用后端表单 - OctoberCMS

Using backend forms in frontend - OctoberCMS

我在后端有一个表单可以创建新条目。我想允许我网站的前端用户也创建这些条目,所以因为我已经在后端有这样的表单和控制器,所以我也想在前端使用它。

我已经为前端创建了一个组件,并尝试在其中呈现后端表单,如下所示:

# components/NewEntryForm.php
<?php namespace Author\PluginName\Components;

use Cms\Classes\ComponentBase;
use Author\PluginName\Models\Entry;
use Author\PluginName\Controllers\Entries as EntryController;

class NewEntryForm extends ComponentBase
{
    public function componentDetails()
    {
        return [
            'name'        => 'NewEntryForm Component',
            'description' => 'No description provided yet...'
        ];
    }

    public function onRun()
    {
        // New back-end form with the context of 'frontend'
        $formController = new EntryController();
        $formController->create('frontend');

        // Append the entryController to the page
        $this->page['form'] = $formController;

        // Add backend styles to the form
        $this->addCss('/modules/backend/assets/css/controls.css', 'core');
    }
}

组件的default.htm:

<!-- components/newentryform/default.htm -->
<form role="form"
    data-request="{{ __SELF__ }}::onSave"
    data-request-success="$el.hide();$('.confirm-container').removeClass('hide');">

    {{ form.formRender()|raw }}

    <div class="form-group">
        <button class="btn btn-primary btn-block btn-lg" type="submit" value="register">Create</button>
    </div>
</form>

但是,我收到以下错误:

An exception has been thrown during the rendering of a template ("The partial '_field_richeditor.htm' is not found.").

您需要 resister 那些 widgets manually ,因为 backend 它们是 registered by backend provider.

Code for registering widgets in frontend, you need to put this code in your plugin's Plugin.php file

// import
use Backend\Classes\WidgetManager;

class Plugin extends PluginBase
{
    public function register() {
      WidgetManager::instance()->registerFormWidgets(function ($manager) {
        // You can add them as per need
        $manager->registerFormWidget('Backend\FormWidgets\RichEditor', 'richeditor');

        // $manager->registerFormWidget('Backend\FormWidgets\CodeEditor', 'codeeditor');          
        // $manager->registerFormWidget('Backend\FormWidgets\MarkdownEditor', 'markdown');
        // $manager->registerFormWidget('Backend\FormWidgets\FileUpload', 'fileupload');
        // $manager->registerFormWidget('Backend\FormWidgets\Relation', 'relation');
        // $manager->registerFormWidget('Backend\FormWidgets\DatePicker', 'datepicker');
        // $manager->registerFormWidget('Backend\FormWidgets\TimePicker', 'timepicker');
        // $manager->registerFormWidget('Backend\FormWidgets\ColorPicker', 'colorpicker');
        // $manager->registerFormWidget('Backend\FormWidgets\DataTable', 'datatable');
        // $manager->registerFormWidget('Backend\FormWidgets\RecordFinder', 'recordfinder');
        // $manager->registerFormWidget('Backend\FormWidgets\Repeater', 'repeater');
        // $manager->registerFormWidget('Backend\FormWidgets\TagList', 'taglist');
        // $manager->registerFormWidget('Backend\FormWidgets\MediaFinder', 'mediafinder');
        // $manager->registerFormWidget('Backend\FormWidgets\NestedForm', 'nestedform');
      });
    }
    // ... other code ...
}

It will register widgets, and you can use them in front-end without any issues.

如有疑问请评论。