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

Freezed default value #170

Open
alejandrogiubel opened this issue Jul 30, 2024 · 9 comments
Open

Freezed default value #170

alejandrogiubel opened this issue Jul 30, 2024 · 9 comments

Comments

@alejandrogiubel
Copy link

Using reactive form generator with freezed the default values ​​that are set to freezed don't seem to work with reactive form, any ideas?

@vasilich6107
Copy link
Contributor

Hey
Could you provide a repo with reproduction code and clear explanation

@alejandrogiubel
Copy link
Author

@just-abdou
Copy link

hey @alejandrogiubel , did you find any solution. Thanks.

@alejandrogiubel
Copy link
Author

alejandrogiubel commented Sep 10, 2024

@just-abdou not yet, just waiting for some update.
A workaround is to use initState callback from generated builder

BusinessModelFormBuilder(
      model: business,
      initState: (context, formModel) {
        if (business == null) {
          formModel.activeControl.updateValue(true);
          formModel.authorizedControl.updateValue(false);
          formModel.productsOrServicesControl.updateValue([]);
        }
      },

@Nolomon
Copy link

Nolomon commented Oct 6, 2024

This also leads to returning null for non-nullable disabled fields when calling formModel.submit() although the disabled field has been annotated with @ Default('').
I'm currently making all fields that might be disabled nullable, not sure if this is the recommended practice.

@vasilich6107
Copy link
Contributor

Here https://github.com/alejandrogiubel/reactive_form_freezed

Checking your example
This is the fix

image

@vasilich6107
Copy link
Contributor

@alejandrogiubel

model: business, => model: business ?? Business()
You need to supply the initial value for the form

@alejandrogiubel
Copy link
Author

Yes, you're right, I hadn't thought about that solution (very obvious btw). But in the case where you have a class with several parameters, including some required ones, it doesn't make much sense to define the required parameters at that moment when the idea is to init them when the form is valid. I think the most appropriate thing is that those class parameters that have a default value are initialized in the control with that value.
For example it doesn't make much sense to do:

Captura de pantalla 2024-10-07 a la(s) 11 11 04 a m

If the idea is that the price is required but without a default value

@vasilich6107
Copy link
Contributor

Currently it is an edgecase that is not handled so nicely
This results in having nullable values the values required from validators point of voew
This will be addressed in v6 where we have separate Output models
You can check the beta version examples

your idea has an edge case.
Imagine that you have 2 required params
If you wish to preset only one of them - the solution will have hard times to help you
Cause you ll not be able to init form with single value
This is especially tricky with date selection
When the value is required but could not be set to any defaults cause dattime does not have const constructor or it is required to be manually selected by user

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants