TextInput default value on edit page
Hi, I want to set a default value on a form text input. I'm adding the
->default('some default string')
property but it only works on Create, in the edit it doesn't works even though the value is empty or null.
For example, I need this for when I add a new field to the form and for example a user accesses the form when they had previously saved it. So that this field appears already filled in with the default value.Solution:Jump to solution
You have many way to do that and you have choose the proper one according to your scenario:
- Use mutator in your Eloquent as @PovilasKorop said.
- Use
->mutateFormDataUsing()
chain or mutateFormDataBeforeSave()
lifecycle method.
- If you need to show the default value to the user, you can use ->formatStateUsing()
and in the callback, check if the $state is null then return you default value...4 Replies
I would save that value in the database on Eloquent level, with mutator
The problem is that I saved the form as json in a field, it is not directly related to a mysql field that can use the mysql default
Solution
You have many way to do that and you have choose the proper one according to your scenario:
- Use mutator in your Eloquent as @PovilasKorop said.
- Use
->mutateFormDataUsing()
chain or mutateFormDataBeforeSave()
lifecycle method.
- If you need to show the default value to the user, you can use ->formatStateUsing()
and in the callback, check if the $state is null then return you default valueok, i'm going to try it