Calculated input field in a form
I wish to include a calculated input field in a form, which is sum of a number of fields, some of which are dynamically included based on an attribute of the record. Although, I would calculate the value in the backend, I want to have the calculated value shown in the form itself while creation.
I've tried
evaluate()
method as well as state()
method both of which give Typed property Filament\Forms\Components\Component::$container must not be accessed before initialization
Is there a way to include such calculated fields?
Alternatively, how I can I include custom javascript only for the form of a particular resource? As then I can calculate the field value via javascript.36 Replies
Solution
have you tried a Placeholder with content(function () {}) ?
Not yet. Thanks for the suggestion.
BTW, I tried
afterStateUpdated
on all the contributing fields, but the value doesn't get updated in the result field without a refresh (such as navigating to other steps in the wizard)its better to do it the other way round where the field calculates its own state based on others instead of other fields setting
placeholder lets you do that
But I need this field's value to be updated whenever any of the field's value is changed. I guess injecting javascript is the only way then. However, I'm not able to figure out how to do it only for specific form or field, and not the entire panel.
make the fields that get changed live() and this will recacluate the placeholder value each time
Ok, thanks!
This works to an extent. But the update becomes a problem, as the field is populated and hence placeholder changes are not visible. I think one way to go about it is creating a pseudo field instead which will never be posted, nor be updated from the record.
i dont understand what the problem is?
the placeholder will be calculated when the form is first loaded
I am talking about record update, when the field will have a value and hence placeholder won't be visible
placeholder fields can be added to the edit page too
Wait, did you mean a field with a placeholder, or a Placeholder field?
Ah, my bad. You were referring to Component/Placeholder and I was working with TextInput->placeholder()
A strange issue though. I'm keeping all these dynamic fields in a JSON column in the db, titled
data
. The column is correctly cast to array, and the data is getting stored in this column alright. However, when updating the record, the corresponding fields such as data.quantity
are not getting populated. I tried changing the cast to object
instead of array
as well.it definitely should be array
please send your schema
the whole thing
are you using an edit page on a resource or is this a custom form
I'm using simple format, so create / edit / view all are in the pop-up
in action modals?
can you send the entire resource then in a github gist? and the model please
if i cant spot anything i'd like you to open an issue with a reproduction repository (a new project with minimal code to reproduce the problem)
Could this be a conflict with the base state path also being called ‘data’ like it’s looking for data.quantity instead of data.data.quantity?
Here is the gist https://gist.github.com/skills-up/8043fef025ea99f8b48227047023c272
Gist
Resource Hierarchy to Allow Multiple Transaction Types
Resource Hierarchy to Allow Multiple Transaction Types - SaleResource.php
if its in a modal the state path isnt data, and even so i dont think that would result in this behaviour
fair, just a thought.
the Sale model please
Let me give you both the models, as Sale is derived from Transaction
also where is getschema called
I've separated schema into a separate method, as that allows me to use the same from other Resources for createOptionForm method, such as
createOptionForm(SaleResource::getSchema())
Now, if my database schema changes, I can update the schema in just one place in the corresponding resource file, and need not search for it in various files which might be using createOptionForm
Just a thought: could array destructuring cause such an issue? https://gist.github.com/skills-up/8043fef025ea99f8b48227047023c272#file-transactionresource-php-L80
Gist
Resource Hierarchy to Allow Multiple Transaction Types
Resource Hierarchy to Allow Multiple Transaction Types - SaleResource.php
shouldnt cause an issue
i cant see whats wrong, so please try in a new project to reproduce it
you might find out what's wrong in the meantime
OK, thanks for trying though. As the data is getting stored in the database, so model appears to be correct. I'm now trying to tinker with it to see if anything jumps out.
where is the editaction?
Using Simple layout, so there is no separate edit layout. Everything is being handled from a single page:
how are you doing the edit then with the information preloaded?
isnt there a button on each table row?
I'm actually extending Resource class which extends Filament\Resources\Resource as BaseResource. Within that I'm using:
Yes, that's exactly what is being done
right so you do have an editaction
add ->mutateRecordDataUsing(fn ($data) => dd($data)), is the data in an array there?
Let me try
No, it's not there. I wonder why.
Ahhhhh
Got it
Data was added to hidden fields
🤦♂️
:pepelaugh:
Yes, that solved it!
Daily reminder of how stupid I can be at times 🥹
its alright, there are a lot of layers here
Thanks so much for your help @Dan Harrin 🙏