F
Filament2w 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(),
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(),
No description
No description
Solution:
Ok i just had a realization I can make it like this. ```php public $disableReturnQuantityRepeater = [];...
Jump to solution
2 Replies
Solution
ico
ico2w ago
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];
}
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];
}
ico
ico2w ago
So in the Toggle afterStateUpddated i modify the $disableReturnQuantityRepeater property and in the Repeater disableItemCreation method i disable the current repeater based on the data inside the $disableReturnQuantityRepeater with is with a structure repeater index => true/false