reactive_forms
reactive_forms copied to clipboard
Field composition
I want to create a field that combines two subfields together. For example, I would like to combine a text field with a dropdown to create a form field for measurements of different kinds (height, weight) where people enter value into the text field and select the right unit from the dropdown.
I want this combined field to behave correctly (up for discussion) like a single field in the following ways:
- have single combined internal state for things like being touched, dirty, valid, error, etc.
- have validator defined on the combined value, not on the individual pieces of data
- have a way to build error in a more customized way (below a row of subfields for instance, instead of the error belonging to just one of the subfields)
Right now, I'm having to do a lot of state tracking myself, and I feel like field composition should be a first class citizen in a form library. Please let me know if you know of a way that's simple, because I couldn't find any.
PS: We can discuss more use cases if we decide to add this as a feature.
Thanks
Have you tried using FormGroup
and FormBuilder
? You can nest as many formGroup elements as you need and use a single validator on the formGroup.
Hi @petoknm You have to create your custom field like https://github.com/joanpablo/reactive_forms/blob/master/lib/src/widgets/reactive_switch.dart
Which will combine two fields into one entity with some object type which will include as many fields as you would like
There is no easy way to compose fields for now
#309 maybe solving your problem?