Filling a form on editPage

i need filling with the previously information of a table, on a edit page, like the discount, modules, and checkbox. I think my problem may be because the first data is from the budget and not from the client, this is because in the create I must have the possibility of creating a budget and a client in the chaos that is chosen. The problem is that when editing it from the table, it does not fill in the data with which it is linked. I have already tried fill(), getChildsContainers, with another form, with eloquent, and it still does not fill them in. If anyone has an idea I would greatly appreciate it. my create code is: public function create(bool $another = false): void { $form = $this->form->getState(); if ($form['budget_client_id']) { $client = BudgetClient::query()->create([ 'email' => $form['email'], 'name' => $form['name'] ?: '', 'last_name' => $form['last_name'] ?: '', 'telephone' => $form['telephone'] ?: '', 'comments' => $form['comments'] ?: '', ]); } Budget::query()->create([ 'budget_client_id' => $client->id ?? null, // Set based on client creation 'modules' => $form['modules'], 'discount' => $form['discount'], 'total_price' => $form['total_price'], ]); $this->redirect(BudgetResource::getUrl()); } There are parts of my code that don't work, such as price persistence, but for now I'm interested in fixing this first. the message is so long, so i give u mi code on another message
No description
No description
No description
16 Replies
JayAyAre
JayAyAreOP10mo ago
public static function form(Form $form): Form { $modules = Module::all(); return $form ->schema([ Forms\Components\Select::make('modules') ->live() ->default('') ->required() ->multiple() ->options(fn() => $modules->pluck('name')) ->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) use ($modules) { $selectedModule = $modules->where('name', $state)->first(); if ($selectedModule) { $discountedPrice = $selectedModule->price * (1 - $get('discount') / 100); $set('total_price', $discountedPrice); } else { $set('total_price', 0); } }), Forms\Components\TextInput::make('discount') ->live() ->numeric() ->minValue(0) ->minValue(0) ->maxValue(100) ->default(0), Forms\Components\Hidden::make('total_price') ->default(0), Forms\Components\Checkbox::make('budget_client_id') ->label('Client') ->live() ->default(false), Forms\Components\Section::make('section') ->label('Client Information') ->schema([ Forms\Components\TextInput::make('name'), Forms\Components\TextInput::make('last_name'), Forms\Components\TextInput::make('email') ->required(fn(Forms\Get $get) => $get('budget_client_id')), Forms\Components\TextInput::make('telephone'), Forms\Components\TextInput::make('comments') ])->visible(fn(Forms\Get $get) => $get('budget_client_id')) ]); } and my edit code is: public function form(Form $form): Form { return $form; }
LeandroFerreira
LeandroFerreira10mo ago
you could use ->formatStateUsing() if you want to override a field state in the edit page
JayAyAre
JayAyAreOP10mo ago
it worked, thanks you so much :DDD
Soundmit
Soundmit10mo ago
i have similar problem in a form i collect user data but the user is a relation (named applicant) when i edit the form instead of the name i have the id so i've made this code

Forms\Components\TextInput::make('user_id')
->label('Il sottoscritto')
->required()
->afterStateHydrated(function (Forms\Components\TextInput $component, $state) {
$component->state(User::find($state)->name);
})
->default($user->name),

Forms\Components\TextInput::make('user_id')
->label('Il sottoscritto')
->required()
->afterStateHydrated(function (Forms\Components\TextInput $component, $state) {
$component->state(User::find($state)->name);
})
->default($user->name),
buth when i save, i get an error because the value need to be an id and not the name
LeandroFerreira
LeandroFerreira10mo ago
Group::make([
TextInput::make('name')
])
->relationship('user')
Group::make([
TextInput::make('name')
])
->relationship('user')
Soundmit
Soundmit10mo ago
tryed but i get Method Filament\Forms\Components\TextInput::relationship does not exist.

->schema([
Forms\Components\TextInput::make('user_id')
->label('Il sottoscritto')
->required()
->relationship('applicant')
->default($user->name),

->schema([
Forms\Components\TextInput::make('user_id')
->label('Il sottoscritto')
->required()
->relationship('applicant')
->default($user->name),
LeandroFerreira
LeandroFerreira10mo ago
sorry, relationship on the Group
Soundmit
Soundmit10mo ago
i don't have the group...do i need to add the group?
LeandroFerreira
LeandroFerreira10mo ago
yep
Soundmit
Soundmit10mo ago
Group::make([
Forms\Components\TextInput::make('user_id')
->label('Il sottoscritto')
->required()
->default($user->name)
])
->relationship('applicant', 'name'),
Group::make([
Forms\Components\TextInput::make('user_id')
->label('Il sottoscritto')
->required()
->default($user->name)
])
->relationship('applicant', 'name'),
this result in empty field this is the relation in the model public function applicant() { return $this->belongsTo(User::class, 'user_id'); }
LeandroFerreira
LeandroFerreira10mo ago
why not a select?
CaptainPrice69
CaptainPrice6910mo ago
Hi, do you have an exemple 😁 ?
Soundmit
Soundmit10mo ago
when the form starts empty the applicant name is pre-filled with the name of the logged in user and it works because the form saves the user id in user_id when the user edit the item i need to fill at least the name with the relation
LeandroFerreira
LeandroFerreira10mo ago
it should be
Group::make([
TextInput::make('name')
])
->relationship('applicant')
Group::make([
TextInput::make('name')
])
->relationship('applicant')
if you want to show the user name. You should use it if you want to set a user_id https://filamentphp.com/docs/3.x/panels/resources/creating-records#customizing-data-before-saving
Soundmit
Soundmit10mo ago
it works! if i understand, the id is managed by the relation
LeandroFerreira
LeandroFerreira10mo ago
what are you trying to do?
Want results from more Discord servers?
Add your server