area17 / twill

Twill is an open source CMS toolkit for Laravel that helps developers rapidly create a custom admin console that is intuitive, powerful and flexible. Chat with us on Discord at https://discord.gg/cnWk7EFv8R.
https://twillcms.com
Apache License 2.0
3.78k stars 575 forks source link

Medias::make() in getCreateForm method not added to payload #2416

Closed agrislaicans closed 9 months ago

agrislaicans commented 9 months ago

Description

In getCreateForm methos at extended BaseModuleController class, adding Medias::make()->name('cover')->label('Cover image') is not saving uploaded image (not saved in twill_mediables table as well as not passed in form payload)

Steps to reproduce

Create a model Blog, add HasMedias trait. In model, define $mediasParams:

    public $mediasParams = [
        'cover' => [
            'default' => [
                [
                    'name' => 'default',
                    'ratio' => 16 / 9,
                ],
            ],
            'mobile' => [
                [
                    'name' => 'mobile',
                    'ratio' => 1,
                ],
            ],
        ],
    ];

Add HandleMedias trait in BlogRepository. Add getCreateForm function in BlogController:

    public function getCreateForm(): Form
    {
        $form = Form::make();

        $form->add(
            Input::make()
                ->name('title')
                ->label('Blog title')
                ->translatable()
                ->required()
        );

        $form->add(
            Input::make()
                ->name('teaser')
                ->label('Teaser')
                ->translatable()
                ->required()
        );

        $form->add(
            Select::make()
                ->name('blog_category_id')
                ->options(BlogCategoryRepository::getListAsSelectOptions())
        );

        $form->add(
            Medias::make()->name('cover')->label('Cover image')
        );

        $form->add(
            Wysiwyg::make()
                ->name('text')
                ->translatable()
                ->toolbarOptions($this->blog_text_features)
                ->note('Full blog text')
                ->limitHeight(false)
                ->required()
        );

        return $form;
    }

Expected result

Selected media image gets passed in form payload and saved in twill_mediables table

Actual result

Every field gets passed in payload, exceptmedias, selected media is not being handled and is not saved in twill_mediables

Versions

Twill version: 3.0.2

Laravel version: 10.42.0

PHP version: 8.1

Database engine: mariadb (11.1.2)

ifox commented 9 months ago

Hi @agrislaicans this is probably missing clarification in the docs but the Medias field is not supported in the create form. Your code should definitely work in the edit form.

If you would like to require that the mediables are stored during the creation of the record instead of during subsequent updates, you can use skipCreateModal() in your setUp method so that the first save will include all fields in the payload. The modal will still show up on top of the edit form, but the record won't be saved until the user actually saves the record.

We could technically support medias and other more complex fields in the create form, but the UX isn't ideal in that modal. Happy to review a PR if you are still looking for the ability to do it knowing the above.

agrislaicans commented 9 months ago

Hi @agrislaicans this is probably missing clarification in the docs but the Medias field is not supported in the create form. Your code should definitely work in the edit form.

If you would like to require that the mediables are stored during the creation of the record instead of during subsequent updates, you can use skipCreateModal() in your setUp method so that the first save will include all fields in the payload. The modal will still show up on top of the edit form, but the record won't be saved until the user actually saves the record.

We could technically support medias and other more complex fields in the create form, but the UX isn't ideal in that modal. Happy to review a PR if you are still looking for the ability to do it knowing the above.

Thanks for such a fast response! Yes, I just discovered this on my own literally as I received notification about your reply.

This actually is perfectly fine and I get your point! Thank you once again.