artflutter / reactive_forms_generator

Other
89 stars 22 forks source link

Freezed default value #170

Open alejandrogiubel opened 3 months ago

alejandrogiubel commented 3 months ago

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 commented 2 months ago

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

alejandrogiubel commented 2 months ago

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

just-abdou commented 1 month ago

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

alejandrogiubel commented 1 month ago

@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 commented 1 month ago

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 commented 4 weeks ago

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

Checking your example This is the fix

image
vasilich6107 commented 4 weeks ago

@alejandrogiubel

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

alejandrogiubel commented 4 weeks ago

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 commented 4 weeks ago

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