Using tabs not repeater for dynamic form

i have multiple database tables with following structure
 articles
 - id
 - author_id - created_at
 - updated_at 
article_localizations
 - id - article_id - locale - title
- body - created_at - updated_at and i have this code for rendering fields for each localization in separate tab.
Forms\Components\Tabs::make('localizations_tabs')
->tabs(function () {
$tabs = [];
$localizations = $this->record->localizations;

foreach ($localizations as $index => $localization) {
$tabs[] = Forms\Components\Tabs\Tab::make($localization->locale)
->schema([
Forms\Components\TextInput::make("localizations.{$localization->id}.locale")
->label('Locale')
->default($localization->locale)
->required(),
Forms\Components\TextInput::make("localizations.{$localization->id}.title")
->label('Title')
->default($localization->title)
->required(),
Forms\Components\RichEditor::make("localizations.{$localization->id}.body")
->label('Body')
->default($localization->body)
->required(),
]);
return $tabs;
}
})
->columnSpanFull(),
Forms\Components\Tabs::make('localizations_tabs')
->tabs(function () {
$tabs = [];
$localizations = $this->record->localizations;

foreach ($localizations as $index => $localization) {
$tabs[] = Forms\Components\Tabs\Tab::make($localization->locale)
->schema([
Forms\Components\TextInput::make("localizations.{$localization->id}.locale")
->label('Locale')
->default($localization->locale)
->required(),
Forms\Components\TextInput::make("localizations.{$localization->id}.title")
->label('Title')
->default($localization->title)
->required(),
Forms\Components\RichEditor::make("localizations.{$localization->id}.body")
->label('Body')
->default($localization->body)
->required(),
]);
return $tabs;
}
})
->columnSpanFull(),
problem is that data not setting in fields. please help to figure out what is wrong. when i use repeater everything works fine, but i need tabs for my implementation and i don't know how to use them with repeater. thank you for help
4 Replies
Patrick1989
Patrick19894w ago
at a glance, don't you have to return the $tabs ?
Forms\Components\Tabs::make('localizations_tabs')
->tabs(function () {
$tabs = [];
$localizations = $this->record->localizations;

foreach ($localizations as $index => $localization) {
$tabs[] = Forms\Components\Tabs\Tab::make($localization->locale)
->schema([
Forms\Components\TextInput::make("localizations.{$localization->id}.locale")
->label('Locale')
->default($localization->locale)
->required(),
Forms\Components\TextInput::make("localizations.{$localization->id}.title")
->label('Title')
->default($localization->title)
->required(),
Forms\Components\RichEditor::make("localizations.{$localization->id}.body")
->label('Body')
->default($localization->body)
->required(),
]);

return $tabs;
}
})
->columnSpanFull()
Forms\Components\Tabs::make('localizations_tabs')
->tabs(function () {
$tabs = [];
$localizations = $this->record->localizations;

foreach ($localizations as $index => $localization) {
$tabs[] = Forms\Components\Tabs\Tab::make($localization->locale)
->schema([
Forms\Components\TextInput::make("localizations.{$localization->id}.locale")
->label('Locale')
->default($localization->locale)
->required(),
Forms\Components\TextInput::make("localizations.{$localization->id}.title")
->label('Title')
->default($localization->title)
->required(),
Forms\Components\RichEditor::make("localizations.{$localization->id}.body")
->label('Body')
->default($localization->body)
->required(),
]);

return $tabs;
}
})
->columnSpanFull()
like this
y_e_v_h_e_n_i_i
sorry this is mistake in this example code. I return $tabs and they renders correctly, but with empty fields just for example screenshot of rendered tabs with form
y_e_v_h_e_n_i_i
seem like i found solution, but it looks not so good
protected function getLocalizationTabs(): array
{
$localizations = $this->record->localizations;

$tabs = [];
$data = [];

foreach ($localizations as $localization) {
$data[$localization->id]['body'] = $localization->body;
$data[$localization->id]['title'] = $localization->title;
$data[$localization->id]['locale'] = $localization->locale;
$tabs[] = Forms\Components\Tabs\Tab::make($localization->locale ?? 'New Localization')
->schema([
Forms\Components\TextInput::make("localizations.$localization->id.locale")
->label('Localization')
->required(),
Forms\Components\TextInput::make("localizations.$localization->id.title")
->label('Title')
->required(),
Forms\Components\Textarea::make("localizations.$localization->id.body")
->label('Body')
->required(),
]);
}

$this->data['localizations'] = $data;

$tabs[] = Forms\Components\Tabs\Tab::make('New Localization')
->schema([
Forms\Components\TextInput::make("new_localization.locale")
->label('Localization')
->required(),
Forms\Components\TextInput::make("new_localization.title")
->label('Title')
->required(),
Forms\Components\Textarea::make("new_localization.body")
->label('Body')
->required(),
]);

return $tabs;
}
protected function getLocalizationTabs(): array
{
$localizations = $this->record->localizations;

$tabs = [];
$data = [];

foreach ($localizations as $localization) {
$data[$localization->id]['body'] = $localization->body;
$data[$localization->id]['title'] = $localization->title;
$data[$localization->id]['locale'] = $localization->locale;
$tabs[] = Forms\Components\Tabs\Tab::make($localization->locale ?? 'New Localization')
->schema([
Forms\Components\TextInput::make("localizations.$localization->id.locale")
->label('Localization')
->required(),
Forms\Components\TextInput::make("localizations.$localization->id.title")
->label('Title')
->required(),
Forms\Components\Textarea::make("localizations.$localization->id.body")
->label('Body')
->required(),
]);
}

$this->data['localizations'] = $data;

$tabs[] = Forms\Components\Tabs\Tab::make('New Localization')
->schema([
Forms\Components\TextInput::make("new_localization.locale")
->label('Localization')
->required(),
Forms\Components\TextInput::make("new_localization.title")
->label('Title')
->required(),
Forms\Components\Textarea::make("new_localization.body")
->label('Body')
->required(),
]);

return $tabs;
}
Want results from more Discord servers?
Add your server