laravel / nova-issues

554 stars 34 forks source link

dependsOn does not work in a Repeatable field #6470

Closed itrack closed 3 months ago

itrack commented 3 months ago

Description:

dependsOn does not work if used in a Repeatable field

Detailed steps to reproduce the issue on a fresh Nova installation:

<?php

namespace App\Nova;

use App\Nova\Repeater\NotificationChannels;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Repeater;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;

class NotificationProfile extends Resource
{

    public static $model = \App\Models\NotificationProfile::class;
    public static $title = 'name';
    public static $search = [
        'name',
    ];

    public function fields(NovaRequest $request)
    {
        return [
            ID::make()->sortable(),
            Text::make('Name')->sortable(),
            Repeater::make('Notification Channels')
                ->repeatables([
                    NotificationChannels::make(),
                ]),
        ];
    }
}
<?php

namespace App\Nova\Repeater;

use App\Models\NotificationChannel;
use Laravel\Nova\Fields\Repeater\Repeatable;
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\FormData;
use Laravel\Nova\Http\Requests\NovaRequest;

class NotificationChannels extends Repeatable
{

    public function fields(NovaRequest $request): array
    {
        return [
            Select::make('Type', 'type')
                ->options([
                    'email' => 'Email',
                    'telegram' => 'Telegram',
                ])
                ->displayUsingLabels(),
            Select::make('Config')
                ->options([
                    'channel' => 'Channel',
                    'private' => 'Private',
                ])
                ->hide()
                ->dependsOn(
                    ['type'],
                    function (Text $field, NovaRequest $request, FormData $formData) {
                        if ($formData->type == 'telegram') {
                            $field->show()->rules('required');
                        }
                    }
                ),
        ];
    }
}
crynobone commented 3 months ago

Duplicate of https://github.com/laravel/nova-issues/discussions/5669

itrack commented 3 months ago

Thank you @crynobone! I understand now, pity, I think this function would have been very useful.