Repeater on edit page

Hi, guys. I am using a Repeater. On edit, if I don't have any items, the repeater only has the button to create items. Is it possible to display the entire repeater form even if I don't have any items? Instead of create button I need that form to be visible on edit without clicking the button
13 Replies
Dan Harrin
Dan Harrin2y ago
you can add a new item to the repeater json in the mutateFormDataBeforeFill() probably?
DianaMujoiu
DianaMujoiuOP2y ago
Hm, a good idea. I'll try. Thanks I got this error "Call to a member function itemLabel() on array" when trying to use mutateRelationshipDataBeforeFill
Dan Harrin
Dan Harrin2y ago
please send the code
DianaMujoiu
DianaMujoiuOP2y ago
Repeater::make('bedroomOptions')
->mutateRelationshipDataBeforeFill(['title' => 'Master bedroom'])
->itemLabel('Bedroom option')
->label('')
->required()
->createItemButtonLabel('Add a room')
->relationship()
->schema([
Repeater::make('bedroomOptions')
->mutateRelationshipDataBeforeFill(['title' => 'Master bedroom'])
->itemLabel('Bedroom option')
->label('')
->required()
->createItemButtonLabel('Add a room')
->relationship()
->schema([
I used mutateRelationshipDataBeforeFill because I got a "doesn't exist mutateRelationshipDataBeforeFill" error
Dan Harrin
Dan Harrin2y ago
it would be on the page class not the repeater but it wont work with relationship(), so dont worry about it ummmm let me think
->afterStateHydrated(function ($component, $state) {
if (count($state ?? [])) {
return;
}

$component->state([[
'field_name_inside_repeater_1' => null,
'field_name_inside_repeater_2' => null,
'field_name_inside_repeater_3' => null,
]]);
})
->afterStateHydrated(function ($component, $state) {
if (count($state ?? [])) {
return;
}

$component->state([[
'field_name_inside_repeater_1' => null,
'field_name_inside_repeater_2' => null,
'field_name_inside_repeater_3' => null,
]]);
})
DianaMujoiu
DianaMujoiuOP2y ago
Should I use this on the repeater, right?
Dan Harrin
Dan Harrin2y ago
yes
IndomieRendang
What about this?
Repeater::make('bedroomOptions')
->afterStateHydrated(function ($context, $state, $component) {
if ($context === 'edit' && ! $state) {
return $component->dispatchEvent('repeater::createItem', $component->getStatePath());
}
})
Repeater::make('bedroomOptions')
->afterStateHydrated(function ($context, $state, $component) {
if ($context === 'edit' && ! $state) {
return $component->dispatchEvent('repeater::createItem', $component->getStatePath());
}
})
DianaMujoiu
DianaMujoiuOP2y ago
None of the above options work. I put a dd inside afterStateHydrated and it doesn't seem to do anything. I am using this Repeater in a custom page
awcodes
awcodes2y ago
Are you calling $this->form->fill() in your mount method?
DianaMujoiu
DianaMujoiuOP2y ago
Yeah This is my mount method
public function mount($record): void
{
$listing = $this->listing = Listing::findOrFail($record);
$this->form->fill([
'bedroom' => $listing->bedroom,
'bathroom' => $listing->bathroom,
'bedroomOptions' => $listing->bedroomOptions,
]);
}
public function mount($record): void
{
$listing = $this->listing = Listing::findOrFail($record);
$this->form->fill([
'bedroom' => $listing->bedroom,
'bathroom' => $listing->bathroom,
'bedroomOptions' => $listing->bedroomOptions,
]);
}
IndomieRendang
I've tested and worked, the only concern in my example is only about the empty $state maybe not ! $state, it can be ! count($state) or smth else... I don't know why dd in afterStateHydrated did nothing
DianaMujoiu
DianaMujoiuOP2y ago
class LayoutAndPricing extends Page implements Forms\Contracts\HasForms
{
use Forms\Concerns\InteractsWithForms;
protected static string $resource = ListingResource::class;
protected static string $view = 'filament.resources.listing-resource.pages.layout-and-pricing';
public $bedroom ;
public $bathroom;
public $bedroomOptions;
public $listing;
public function mount($record): void
{
$listing = $this->listing = Listing::findOrFail($record);
$this->form->fill([
'bedroom' => $this->bedroom,
'bathroom' => $listing->bathroom,
'bedroomOptions' =>$listing->bedroomOptions
]);
}
public function getFormModel(): Listing
{
return $this->listing;
}x
public function back()
{
$success = $this->listing->update($this->form->getState());
if ($success) {
$record = $this->listing->id;
return redirect('/admin/listings/create/address-details/'. $record);
}
public function save()
{
$success = $this->listing->update($this->form->getState());
if ($success) {
$record = $this->listing->id;
return redirect('/admin/listings/create/amenities/'. $record);
}
protected function getFormSchema(): array
{
return [
Repeater::make('bedroomOptions')
->lazy()
->itemLabel('Bedroom option')
->afterStateHydrated(function ($context, $state, $component) {
if ($context === 'edit' && ! count($state)) {
return $component->dispatchEvent('repeater::createItem', $component->getStatePath());
}
->relationship()
->schema([
class LayoutAndPricing extends Page implements Forms\Contracts\HasForms
{
use Forms\Concerns\InteractsWithForms;
protected static string $resource = ListingResource::class;
protected static string $view = 'filament.resources.listing-resource.pages.layout-and-pricing';
public $bedroom ;
public $bathroom;
public $bedroomOptions;
public $listing;
public function mount($record): void
{
$listing = $this->listing = Listing::findOrFail($record);
$this->form->fill([
'bedroom' => $this->bedroom,
'bathroom' => $listing->bathroom,
'bedroomOptions' =>$listing->bedroomOptions
]);
}
public function getFormModel(): Listing
{
return $this->listing;
}x
public function back()
{
$success = $this->listing->update($this->form->getState());
if ($success) {
$record = $this->listing->id;
return redirect('/admin/listings/create/address-details/'. $record);
}
public function save()
{
$success = $this->listing->update($this->form->getState());
if ($success) {
$record = $this->listing->id;
return redirect('/admin/listings/create/amenities/'. $record);
}
protected function getFormSchema(): array
{
return [
Repeater::make('bedroomOptions')
->lazy()
->itemLabel('Bedroom option')
->afterStateHydrated(function ($context, $state, $component) {
if ($context === 'edit' && ! count($state)) {
return $component->dispatchEvent('repeater::createItem', $component->getStatePath());
}
->relationship()
->schema([
Maybe it's something wrong with this page?
Want results from more Discord servers?
Add your server