-
PackageForm builder Package Versionv3.2.133 How can we help you?Hi I could not find how to add default items to a relationship repeater. I the EditPage for TimesheetWeek I want to display 7 TimesheetDay even if they do not exist in database. There is 2 tables: class TimesheetWeek extends Model {
protected $casts = [
'first_day' => 'immutable_date',
];
public function days(): HasMany {
return $this->hasMany( TimesheetDay::class, 'week_id','id' );
}
}
class TimesheetDay extends Model
{
protected $casts = [
'day' => 'immutable_date',
'hours' => 'json',
];
public function week(): BelongsTo
{
return $this->belongsTo( TimesheetWeek::class, 'week_id','id' );
}
} In the /** @var CarbonImmutable $first_day */
$first_day = $form->getRecord()->first_day;
return $form
->schema([
Forms\Components\Fieldset::make('Week')
->schema([
Forms\Components\Select::make('company_id')
->relationship('company', 'name')
->required(),
Forms\Components\DatePicker::make('first_day')
->required(),
]),
Repeater::make('days')
->relationship()
->columnSpan(2)
->schema([
Forms\Components\DatePicker::make('day')
->required(),
Repeater::make('hours')
->schema([
Forms\Components\Fieldset::make('Label')
->schema([
TextInput::make('start')->required(),
TextInput::make('end')->required(),
])
])
->grid(4)
// ->defaultItems(2), // only works on CreatePage.
->default([
['start' => '', 'end' => ''],
['start' => '', 'end' => ''],
['start' => '', 'end' => ''],
])
,
])
//->defaultItems(7), // only works on CreatePage.
->default([
['day'=>$first_day],
['day'=>$first_day->addDay(1)],
//new TimesheetDay(['day'=>$first_day]),
//new TimesheetDay(['day'=>$first_day->addDay(1)]),
])
,
]); Every things work fine, excepted the Any idea ? I've seeked the Internet for hours around mutateRelationshipDataBeforeFillUsing, afterStateHydrated ... but could not find a working solution 😥 Thanks for your help 💌 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
yes, default only works on CreatePage. Use formatStateUsing |
Beta Was this translation helpful? Give feedback.
Ok, thanks @leandrocfe 👍
I use afterStateHydrated() to add default data to
Repeater::make('day')
and remove the call toRepeater::make('hours')->default([...])