Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working in repeatables #50

Open
mgolshan opened this issue Oct 12, 2024 · 0 comments
Open

Not working in repeatables #50

mgolshan opened this issue Oct 12, 2024 · 0 comments

Comments

@mgolshan
Copy link

When I use this in repeatable fields, it works fine while creating. But the data are missing after saving.
And, when I edit the created resource, the dependency effect applies to all fields.

Example code:
The repeatable field:

class FormSchemaField extends Repeatable
{
    use HasDependencies;

    public const TYPES_WITHOUT_ATTR = [
        OrderForm::FIELD_TYPE_LABEL,
        OrderForm::FIELD_TYPE_DIVIDER,
    ];

    public const TYPES_WITHOUT_PLACEHOLDER = [
        OrderForm::FIELD_TYPE_CHECKBOX,
        OrderForm::FIELD_TYPE_RADIO,
    ];

    public const TYPES_WITH_OPTIONS = [
        OrderForm::FIELD_TYPE_DROPDOWN,
        OrderForm::FIELD_TYPE_RADIO,
    ];

    public function fields(NovaRequest $request)
    {
        return [
            Select::make('Field Type', 'type')->required()->rules('required')
                  ->searchable()
                  ->options(OrderForm::VALID_FIELD_TYPES)->displayUsing(fn($value) => OrderForm::VALID_FIELD_TYPES[$value] ?? 'N/A'),
            Text::make('Field Label', 'label')->required()->rules('required'),
            Hidden::make('Slug', 'slug')->withMeta(['value' => Str::slug($request->input('label'))]),

            DependencyContainer::make(
                [
                    Boolean::make('Required?', 'required')->default(false),
                    Text::make('Help Text', 'helpText')->nullable(),
                ]
            )->dependsOnNotIn('type', self::TYPES_WITHOUT_ATTR),

            DependencyContainer::make(
                [
                    KeyValue::make('Options', 'options')
                        ->fullWidth()
                        ->rules('required', 'array', 'min:1')
                        ->keyLabel('Option (Desired Value)')
                        ->valueLabel('Label')
                        ->actionText('Add Option'),
                ]
            )->dependsOnIn('type', self::TYPES_WITH_OPTIONS),

            DependencyContainer::make(
                [
                    Text::make('Placeholder', 'placeholder')->nullable(),
                ]
            )->dependsOnNotIn('type', array_merge(self::TYPES_WITHOUT_PLACEHOLDER, self::TYPES_WITHOUT_ATTR)),

            DependencyContainer::make(
                [
                    Number::make('Min', 'min'),
                    Number::make('Max', 'max'),
                ]
            )->dependsOn('type', OrderForm::FIELD_TYPE_NUMBER),

In the resource file:

            Repeater::make('Fields', 'schema')
                ->repeatables([
                    FormSchemaField::make(),
                    ]
                )->asJson(),

Also, schema is cast as array in the model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant