TextInput default value on edit page
Hi, I want to set a default value on a form text input. I'm adding the
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.
->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
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 ormutateFormDataBeforeSave()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