Open alejandrogiubel opened 3 months ago
Hey Could you provide a repo with reproduction code and clear explanation
hey @alejandrogiubel , did you find any solution. Thanks.
@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([]);
}
},
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.
Here https://github.com/alejandrogiubel/reactive_form_freezed
Checking your example This is the fix
@alejandrogiubel
model: business,
=> model: business ?? Business()
You need to supply the initial value for the form
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:
If the idea is that the price is required but without a default value
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
Using reactive form generator with freezed the default values that are set to freezed don't seem to work with reactive form, any ideas?