FilamentF
Filament2y ago
ico

Filament v2 Form Builder Disable inner repeater

Hello

I have a repeater inside a repeater

I have a Toggle button witch i want when i press to remove all of the elements of the repeater ( R2 ) and remove the Add button.
For now i have removed the elements from the repeater ( R2 ) but i can't remove the Add button

Repeater::make('returnGoods') // R1
    ->label('')
    ->schema([
        TextInput::make('returnGoodCode')
            ->label(__('good.code'))
            ->disabled(),
        TextInput::make('returnGoodName')
            ->label(__('good.goodName'))
            ->disabled()
            ->columnSpan(2),
        Toggle::make('returnGoodsCheckbox')
            ->label('No return goods ?')
            ->inline(false)
            ->reactive()
            ->afterStateUpdated(function (Toggle $component, Closure $get, Closure $set, $state) {
                if ($state) {
                    // this will return something like 'returnGoods.0.returnGoodsCheckbox'
                    $repeaterPath = $component->getStatePath();
                    $repeaterPathExploded = explode('.', $repeaterPath);
                    $repeaterIndex = $repeaterPathExploded[1];
                    $this->returnGoods[$repeaterIndex]['returnQuantities'] = [];
                }
            }),

        Repeater::make('returnQuantities')
            ->schema([
                TextInput::make('quantity')
                    ->label('Quantity')
                    ->numeric()
                    ->default('0')
                    ->step('0.001'),
            ])
            ->columnSpanFull()
            ->disableItemMovement()
            ->disableItemDeletion()
            ->createItemButtonLabel('Add'),

        ViewField::make('printButton')
            ->label('')
            ->view('components.filament.barcode-print', ['eventName' => 'printBarcodes'])
            ->columnSpanFull(),
    ])
    ->columns(4)
    ->columnSpanFull()
    ->disableItemMovement()
    ->disableItemCreation()
    ->disableItemDeletion(),
image.webp
image.webp
Solution
Ok i just had a realization

I can make it like this.
public $disableReturnQuantityRepeater = [];

.
.
.

// toggle 
->afterStateUpdated(function (Toggle $component, $state) {

       $repeaterIndex = $this->getPathIndex($component);

        if ($state) {

            $this->returnGoods[$repeaterIndex]['returnQuantities'] = [];
            $this->disableReturnQuantityRepeater[$repeaterIndex] = true;

            return;
        }

        $this->disableReturnQuantityRepeater[$repeaterIndex] = false;
    }),
.
.
.

// repeater
->disableItemCreation(function (Repeater $component) {

        $repeaterIndex = $this->getPathIndex($component);

        if (array_key_exists($repeaterIndex, $this->disableReturnQuantityRepeater)) 
            return $this->disableReturnQuantityRepeater[$repeaterIndex];
    })

.
.
.
.

protected function getPathIndex(mixed $component): int|string
{
    // this will return something like 'returnGoods.0.returnGoodsCheckbox'
    $componentPath = $component->getStatePath();
    $componentPathExploded = explode('.', $componentPath);

    return $componentPathExploded[1];
}
Was this page helpful?