Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FormBuilderTextField sets field state to '' even though transformer sets return to null #1350

Closed
2 of 7 tasks
john-rager opened this issue Jan 17, 2024 · 6 comments · Fixed by #1423
Closed
2 of 7 tasks
Labels
bug Something isn't working

Comments

@john-rager
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Package/Plugin version

9.1.1

Platforms

  • Android
  • iOS
  • Linux
  • MacOS
  • Web
  • Windows

Flutter doctor

Flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.16.3, on Microsoft Windows [Version 10.0.22621.3007], locale en-US)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[√] Chrome - develop for the web
[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.5.3)
[√] Android Studio (version 2021.2)
[√] VS Code (version 1.85.1)
[√] Connected device (3 available)
[√] Network resources

• No issues found!

Minimal code example

Code sample
FormBuilderTextField(
                  name: 'writeSize',
                  decoration: const InputDecoration(
                    labelText: 'Write Size (bytes)',
                  ),
                  initialValue: writeSize.toString(),
                  validator: FormBuilderValidators.compose(
                    [
                      FormBuilderValidators.integer(),
                      FormBuilderValidators.min(1),
                      FormBuilderValidators.max(128),
                    ],
                  ),
                  valueTransformer: (value) => int.tryParse(value!),
                ),

Current Behavior

Trying to use FormBuilderTextField to create a field that accepts either an integer or nothing. The int.tryParse call inside valueTransformer returns null if value is an empty string, however the field state gets set to an empty string rather than null. This forces me to transform the transform so that I can get to the needed null.

While experimenting I took the int.tryParse out of the equation by transforming to null no matter what, e.g. valueTransformer: (value) => null. The field state is still changed to ''.

Expected Behavior

If I am intentionally transforming the value to null, I expect the field state to be null and not an empty string. Am I missing a necessary property?

Steps To Reproduce

Incorporate the above field widget sample into a form and try it out.

Aditional information

I know I can work around this by transforming all of the empty integer fields to null on submit, but this should be unnecessary. Please let me know if this can be fixed or if I'm missing something.

@john-rager john-rager added the bug Something isn't working label Jan 17, 2024
@john-rager
Copy link
Author

I've dug into this further and it appears that if the field started out as null and was left null, it works as expected on save - I get a null value and not an empty string. However, if the field is "touched", e.g. I empty a field that has a value or I enter some data and then empty it, the valueTransformer runs and sets the value to null but the currentState.values entry for the field gets set to an empty string at some point between valueTransformer and saveAndValidate.

@novaware-io
Copy link

same issue for me, and it makes my serialiser fails: even if the transformer correctly returns null, it still gets saved as '', from a quick check I think it take the null as a return value for "do not transform" and it applies the current value, I think this is wrong in terms of logic as it doesn't work with the nullable fields

@marco-origam
Copy link

Im having the same issue, also i pointed it out on the discussions:

#1286

@BrottarBasse
Copy link
Contributor

I have looked through the code and the issue is indeed with how the valueTransformer is implemented. As i am not too familiar with the library and there seem to be some dead code in regards to the valueTransformer I am reluctant to make a pull request but could do so if noone else has the time to look through this. The problem is in every instance where the transformer is used:

_transformers[key]?.call(value) ?? value))

This works fine as long as the transformer doesn't transform the value to null. If the transformer however transform the value to null the evaluation will use the original value instead

To fix this changing the above code to:

_transformers[key] == null ? value : _transformers[key]!(value)

Here the transformed value will be used even if it transforms the value to null.

@andresthinkme
Copy link
Contributor

Does anybody know when this will be available in a new version?

@deandreamatias
Copy link
Collaborator

Hi @andresthinkme!
If you can create a commit like here and open MR, after merge I can create a new version automatically by adding a tag to the merge commit.

deandreamatias added a commit that referenced this issue Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants