artflutter / reactive_forms_generator

Other
89 stars 22 forks source link

How to use ControlValueAccessor with Reactive forms generator? #168

Open Dionnie123 opened 3 months ago

Dionnie123 commented 3 months ago

I use freezed + reactive forms generator and I have this color picker field.

    @RfControl()
    @Default(Colors.red)
    Color colorPick,

I'm struggling to integrate the ControlValueAccessor with my form management and database saving processes. Despite using reactive_forms_generator to generate form controls, I find it challenging to exert control over how values are handled, particularly with saving data to the database.

vasilich6107 commented 2 months ago

Control value accessors are used to convert values from form state to control consumable format and back. They are not for retrieving the values.

vasilich6107 commented 2 months ago

Use .model getter to retrieve your model https://github.com/artflutter/reactive_forms_generator/blob/master/packages/reactive_forms_generator/example/lib/docs/group/group_form.dart#L152

And then proceed with saving

vasilich6107 commented 2 months ago

Control value accessors are used with fields, and not with generator

Dionnie123 commented 2 months ago

@vasilich6107 reactive color picker was built to accept Color type and return Color type, what approach do you recommend if I want the color field to accept Color type and return int type or vice-versa? should I make my custom color picker from scratch?

vasilich6107 commented 2 months ago

Color could be created from int const Color(int value)

and Colors.red.value returns int

so you can retrieve int value from color when putting the item to database using value