F
Filament15mo ago
Chriis

Generating the value of an input from multiple input

Hi, is it possible to generate the value of an input from multiple input that are in the same form ? The slug example in the documentation is only for generating from one input (https://filamentphp.com/docs/3.x/forms/advanced#generating-a-slug-from-a-title) But I want to aggregate different form values into one 😦
39 Replies
toeknee
toeknee15mo ago
You would just get them all in the final one. If you want to allow editting then you would have them be reactive, lazy and then
->afterStateUpdated(function($get, $set) { $set('my_combined_field', $get('value1') . $get('value2'))
->afterStateUpdated(function($get, $set) { $set('my_combined_field', $get('value1') . $get('value2'))
But if you don't want them to edit it, then just do it on the mutatebefore save
Chriis
ChriisOP15mo ago
My field is disabled but users can see the string being formed I don't understand where I put this, multiple fields are updating the state of the 'code' field Oh ok If I understand, only one field update the 'code' field
toeknee
toeknee15mo ago
So you apply it to each one that is updating the field
Chriis
ChriisOP15mo ago
Ok, and its not a problem if in the concantenation a field is missing because the user is only at the first one currently ?
toeknee
toeknee15mo ago
Doesn't matter at all, it'll just be empty
Chriis
ChriisOP15mo ago
Ok thx 🙂
toeknee
toeknee15mo ago
standard getter and setters
Chriis
ChriisOP15mo ago
@toeknee I have another problem. These fields (concatenate into 1 field) are all select with a relationship (1-n), so in the disabled field, its the id of the select value which is transmitted. How can I choose which column I want ?
toeknee
toeknee15mo ago
Please provide the form code and confirm which fields you are wanting
Chriis
ChriisOP15mo ago
Ok, so for some context :
No description
Chriis
ChriisOP15mo ago
The form
No description
Chriis
ChriisOP15mo ago
No description
Chriis
ChriisOP15mo ago
So 3 of these fields are 1-n relationship with this SheetResource
Chriis
ChriisOP15mo ago
The field receiving the concatenation :
No description
Chriis
ChriisOP15mo ago
So for example the cours select show me the name (like specified in the relationship) but for concatenation I want the acronym And its the same pattern for the other 2 Except maybe for the 'team' select, where I want to have the foreign key of the record (discipline_id), but its shouldnt be harder than an actual column of the table I think
toeknee
toeknee15mo ago
So why not just fetch all the fields and condition on their values? array_filter to remove the empty's and then implode(' ', $arrayOfTheValues) ?
Chriis
ChriisOP15mo ago
Something like that for every field ? for the fetch part
No description
toeknee
toeknee15mo ago
Yep that would work You can always build a function to handle this code, where you just pass the getter and setter into too,.
Chriis
ChriisOP15mo ago
Yeah it will be better, and where can I put thsi fucntion ? In the resource or with my wizard in my CreateRecord class ?
toeknee
toeknee15mo ago
In the class of course
Chriis
ChriisOP15mo ago
I'm struggling doing something clean 😅 I'm sooo bad
Chriis
ChriisOP15mo ago
@toeknee Sorry to spam 😬, but last question : Is it possible to make this fetch in 1 step ? Getting the code with the team_id
No description
toeknee
toeknee15mo ago
build a relationship on teams for disceplines and use:
Sheets::find($get('sheet_id'))->team?->disciplines?->code
Sheets::find($get('sheet_id'))->team?->disciplines?->code
Chriis
ChriisOP15mo ago
It doesnt work 😦
toeknee
toeknee15mo ago
So debug it, on each relationship dd(Sheets::find($get('sheet_id'))); if it works, dd on the next relation i.e. ->team, then -> disciplines etc
Chriis
ChriisOP15mo ago
It fails on $get('sheet_id'), because I don't have a 'sheet_id' field and even if I had one, I can't have an id before saving the record So I don't really understand how this solution is possible Anyway I'm probably going to stay on my solution of querying both tables for now ... thanks for helping 🙂
toeknee
toeknee15mo ago
So where is sheets loaded from
Chriis
ChriisOP15mo ago
To be clear, I'm working on the SheetCreation wizard here
toeknee
toeknee15mo ago
Ok that's different, so you are on a sheet but it hasn't been created yet? So a team isn't asigned yet? So you need to get the current users team or the selected team within the sheet creation from there you can load the team and then load disciplines
Chriis
ChriisOP15mo ago
Yes exactly, its what I've done I struggle at this step What do you mean by loading ?
toeknee
toeknee15mo ago
with the model.....
Team::find($get('team_id', auth()->user()->current_team_id)->disciplines
Team::find($get('team_id', auth()->user()->current_team_id)->disciplines
and ensure you have the disciplines relationship on the teams model.
Chriis
ChriisOP15mo ago
Well yes but I still don't reach the 'code' with that no ?
toeknee
toeknee15mo ago
No, but the code will be within the array of diciplines i.e.
Team::find($get('team_id', auth()->user()->current_team_id)->disciplines[0]->code
Team::find($get('team_id', auth()->user()->current_team_id)->disciplines[0]->code
assuming disciplines is an array of disciplines
Chriis
ChriisOP15mo ago
Its a BelongTo relation
No description
toeknee
toeknee15mo ago
So you just use:
Team::find($get('team_id', auth()->user()->current_team_id)?->discipline?->code
Team::find($get('team_id', auth()->user()->current_team_id)?->discipline?->code
The above assumes you have a current_team_id on the user object and / or that you are selecting the team within the form
Chriis
ChriisOP15mo ago
Ok so I'm not currently treating the current_team case but its not changing the core query, so for only the selected team I do :
No description
Chriis
ChriisOP15mo ago
No description
Chriis
ChriisOP15mo ago
For 'discipline', you are refering to the relationship method ? Because php really dont like that and I don't know why 😦
toeknee
toeknee15mo ago
? checks if there is a value. You can remove it, but then you need to check the team exists first. It's basic PHP you should be able to write a simple function with many if statements to be sure.
Want results from more Discord servers?
Add your server