whitecube / nova-flexible-content

Flexible Content & Repeater Fields for Laravel Nova
MIT License
780 stars 229 forks source link

Fix Polymorphic Relationships #440

Open Sauruz opened 1 year ago

Sauruz commented 1 year ago

In polymorphic relationships the nested content is not decoded/casted to an array.

This fix will test again if the nested json object can be decoded to an array and will set the keys correct.

See: https://github.com/whitecube/nova-flexible-content/issues/435

Screenshot 2023-02-16 at 10 04 53

toonvandenbos commented 1 year ago

Hi @Sauruz, can you give some more details about the exact workflow inside Nova triggering this issue? In #435 you're talking about MorphOne, could you add screenshots or a concrete example? Thanks.

Keko-94 commented 1 year ago

@Sauruz Thank you for the fix but the validations doesn't work properly. Try to add required in your meta_title and check if it passes.

@toonvandenbos you can reproduce the bug like this: MorphOne::make('Quiz', 'qcm', Qcm::class)

Nova\Qcm.php

public function fields(NovaRequest $request): array
    {
        return [
            Text::make(__('Label'), 'label')
                ->filterable()
                ->sortable()
                ->rules('required'),

            Textarea::make(__('Description'), 'description'),

            Flexible::make('Questions', 'questions')
                ->button(trans_choice(__('Add a new :resource'), 1, ['resource' => 'question']))
                ->addLayout(QuestionLayout::class)
                ->resolver(QuestionResolver::class)
                ->confirmRemove(trans_choice(__('Are you sure you want to delete this :resource ?'), 1, ['resource' => 'question']), __('Yes'), __('No'))
                ->rules('required')
                ->limit(10)
                ->fullWidth()
                ->collapsed(true)
        ];
    }

Nova\Flexible\Layouts\QuestionLayout.php

public function fields(): array
    {
        return [
            Text::make('Question', 'label')
                ->rules('required'),

            Textarea::make(__('Explanation'), 'explanation')
                ->rows(3),

            Flexible::make(__('Answers'), 'answer--nested-flexible')
                ->button(trans_choice(__('Add a new :resource'), 1, ['resource' => strtolower(__('Answer'))]))
                ->addLayout(AnswerLayout::class)
                ->resolver(AnswerResolver::class)
                ->confirmRemove(trans_choice(__('Are you sure you want to delete this :resource ?'), 1, ['resource' => strtolower(__('Answer'))]), __('Yes'), __('No'))
                ->rules('required', new MinFlexibleLayoutsRule(2))
                ->limit(10)
                ->fullWidth()
                ->collapsed(true)
        ];
    }