Add EmptyString support for Formik compatibility #955
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add EmptyString support for Formik compatibility
Description
Added support for generating Data Transfer Objects (DTOs) with empty string values instead of null values. This enhancement is particularly useful when working with Formik and Inertia.js in Laravel applications.
Features
EmptyStringData
trait and interfaceEmptyDataResolver
with empty string supportemptyString()
that returns DTO with empty strings as default valuesUse Case
When working with Formik forms in React + Inertia.js + Laravel applications, you often need to provide initial values for form fields. Using null values with Formik can cause errors as Formik expects string values for its form fields. Previously, you would need to:
Now you can simply use:
This is particularly beneficial for large DTOs where manually defining empty values for each field would be tedious and error-prone.
Example with formik, spatie/laravel-typescript-transformer and inertia.js:
Controller:
Changes
EmptyStringData
trait and interfaceEmptyDataResolver
withdefaultValue
parameterData
class to implementEmptyStringData
Breaking Changes
None. This is a backwards-compatible enhancement.