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
16 Replies
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;
}
you could use
->formatStateUsing()
if you want to override a field state in the edit pageit worked, thanks you so much :DDD
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
buth when i save, i get an error because the value need to be an id and not the name
tryed but i get
Method Filament\Forms\Components\TextInput::relationship does not exist.
sorry, relationship on the Group
i don't have the group...do i need to add the group?
yep
this result in empty field
this is the relation in the model
public function applicant()
{
return $this->belongsTo(User::class, 'user_id');
}
why not a select?
Hi, do you have an exemple 😁 ?
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
it should be
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
it works!
if i understand, the id is managed by the relation
what are you trying to do?