Ad

Add Fileupload Fields Into October Cms

- 1 answer

I want to custom backend_users by add image field. I extend model and form field in boot method like this:

public function boot()
{
    BackendUserModel::extend(function($model) {
        $model->attachOne['image'] = \System\Models\File::class;
    });

    BackendUserController::extendFormFields(function($form, $model, $context) {
        $form->addTabFields([
            'image' => [
                'label' => 'image',
                'type' => 'fileupload',
                'tab' => 'image'
            ],
       ]);
   });
}

And it shows error:

Model 'System\Models\File' does not contain a definition for 'image'.

What did I do something wrong? Please help me. Thanks!

Ad

Answer

Problem

Your BackendUserController::extendFormFields code is extending each and every form which is on your BackendUserController.

So to be sure there are 2 forms

  1. Backend User's Form
  2. avatar field's \System\Models\File From

So, your code is basically adding image field to both forms so we are getting error for 2nd form Model 'System\Models\File' does not contain a definition for 'image'.

Solution

To avoid that we just need to make sure we are adding field to correct model by adding condition.

\Backend\Controllers\Users::extendFormFields(function($form, $model, $context) {

    // Only for the backend User model we need to add this
    if ($model instanceof \Backend\Models\User) { // <- add this condition
        $form->addTabFields([
            'image' => [
                'label' => 'image',
                'type' => 'fileupload',
                'tab' => 'image'
            ],
        ]);
    }
});

Now it should only add image field for backend user model and your code should work perfectly.

if any doubts please comment.

Ad
source: stackoverflow.com
Ad