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
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
But if you don't want them to edit it, then just do it on the mutatebefore save
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
So you apply it to each one that is updating the field
Ok, and its not a problem if in the concantenation a field is missing because the user is only at the first one currently ?
Doesn't matter at all, it'll just be empty
Ok thx 🙂
standard getter and setters
@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 ?
Please provide the form code and confirm which fields you are wanting
Ok, so for some context :
The form
So 3 of these fields are 1-n relationship with this SheetResource
The field receiving the concatenation :
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
So why not just fetch all the fields and condition on their values? array_filter to remove the empty's and then implode(' ', $arrayOfTheValues) ?
Something like that for every field ? for the fetch part
Yep that would work
You can always build a function to handle this code, where you just pass the getter and setter into too,.
Yeah it will be better, and where can I put thsi fucntion ?
In the resource or with my wizard in my CreateRecord class ?
In the class of course
I'm struggling doing something clean 😅 I'm sooo bad
@toeknee Sorry to spam 😬, but last question : Is it possible to make this fetch in 1 step ? Getting the code with the team_id
build a relationship on teams for disceplines and use:
It doesnt work 😦
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
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 🙂
So where is sheets loaded from
To be clear, I'm working on the SheetCreation wizard here
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
Yes exactly, its what I've done
I struggle at this step
What do you mean by loading ?
with the model.....
and ensure you have the disciplines relationship on the teams model.
Well yes but I still don't reach the 'code' with that no ?
No, but the code will be within the array of diciplines i.e.
assuming disciplines is an array of disciplines
Its a BelongTo relation
So you just use:
The above assumes you have a current_team_id on the user object and / or that you are selecting the team within the form
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 :
For 'discipline', you are refering to the relationship method ? Because php really dont like that and I don't know why 😦
? 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.