octobercmsoctobercms-backendoctober-form-controlleroctober-partial

Using backend forms in frontend - OctoberCMS


I have a form in the back-end to create new entries. I want to allow my website's front-end users to create these entries as well, so since I already have such form and the controller in the back-end, I'd like to utilize it in the front-end as well.

I've created a component for the front-end and tried to render the back-end form in it, like so:

# 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');
    }
}

Component's 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>

However, I get the following error:

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


Solution

  • You need to resister those widgets manually , as in backend they are 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.

    if any doubts please comment.