Search code examples
phplaraveloctobercmsoctobercms-backend

Dynamic formConfig in same controller in October CMS


I am trying to make a car rental agency, and in the backend I want to be able to set the rates. However there are two different (but related) ways to do this, either by individual date, or in bulk, by selecting a date range and looping over the individual dates.

In the controller I have two actions defined to do this, calendar() and bulk() respectively. I also choose the form fields yaml file to be loaded by setting the $formConfig public property. My controller looks something like this:

class AvailableCars extends Controller
{
    public $formConfig = 'config_form.yaml';

    public function bulk($recordId = null, $context = null)
    {
        $this->pageTitle = "Bulk update rates";
        $model = $this->formFindModelObject($recordId);
        $this->initForm($model);
    }

    public function calendar($recordId = null, $context = null)
    {
        $this->pageTitle = "Update rates for single date on calendar";
        $model = $this->formFindModelObject($recordId);
        $this->initForm($model);
    }

    public function onSave($recordId = null, $context = null)
    {
        $this->formFindModelObject($recordId)->save();
        Flash::success("Rates saved successfully");
    }
}

The problem is that this works for one of the actions, however if I put, for example:

$this->formConfig = 'alternate_fields.yaml';

in either of the bulk() or calendar() methods, it does not override the behavior and load a different form config yaml file; and it even errors out if it is not previously defined as a class property. So I can assume this yaml file is loaded before either of these methods are called.

So my question is, is there any way to load a dynamic formConfig yaml file based on the entry point? Or, is this even good practice in Laravel / October, or should each controller only be responsible to do one thing, and have only one way to create/read/update/destroy a model?


Solution

  • You can set configuration file manually as per your need. but we also need to follow some rules and add required methods with proper naming conventions for it. and yes every thing is good practice if we do it properly :)

    You can use this code for update existing records.

    public function updatebulk($recordId = null, $context = null)
    {
        $this->pageTitle = "Bulk update rates";
        $this->asExtension('FormController')->setConfig('bulk_config.yaml');
        $this->asExtension('FormController')->update($recordId, $context);
    }
    
    public function updatebulk_onSave($recordId = null, $context = null) {
        $this->asExtension('FormController')->setConfig('bulk_config.yaml');
        $this->asExtension('FormController')->update_onSave($recordId, $context);
        // or custom logic (if you add custom logic please remove above lines)
    }
    

    Now you can navigate to http://localhost/backend/author/plugin/controller_name/updatebulk/1 it will render form based on new bulk_config.yaml configuration file.

    and when you save it, it will call updatebulk_onSave to update record. we must follow rule and let FormController handle all control to make it work correctly.

    If you need to save record differently then you need to add custom logic in updatebulk_onSave method its up to you.

    @Note

    If you also need creation functionality you need additional methods. For ex.

    public function createbulk($context = null)
    {
        $this->pageTitle = "Bulk create rates";
        $this->asExtension('FormController')->setConfig('bulk_config.yaml');
        $this->asExtension('FormController')->create($context);
    }
    
    public function createbulk_onSave($context = null) {
        $this->asExtension('FormController')->setConfig('bulk_config.yaml');
        $this->asExtension('FormController')->create_onSave($context);
    }
    

    if any doubts please comment.