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

FormArray with FormGrop(s) dont work with .patchValue() #452

Open
ShyshkovOleg opened this issue May 14, 2024 · 1 comment
Open

FormArray with FormGrop(s) dont work with .patchValue() #452

ShyshkovOleg opened this issue May 14, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@ShyshkovOleg
Copy link

ShyshkovOleg commented May 14, 2024

Hi, @joanpablo. Thanks for you package it is great.
Im facing one issue with FormArray of FormGroups. .patchValue() did not patch and .updateValue() throws an error.
Could you please:

  1. explain how to use FormArray of FormGroups correctly;
  2. could I use FormControl instead of FormControlName with FormArray of FormGroups
class _NotificationsState extends State<Notifications> {
  FormGroup optionForm =
      FormGroup({'options': FormArray<Map<String, dynamic>>([])});

  FormArray get optionsFormArray {
    return optionForm.control('options') as FormArray;
  }
  @override
  void initState() {
    optionForm.patchValue({
      'options': [
        {'name': "option #1", 'value': '#1 value'},
        {'name': "option #2", 'value': '#2 value'}
      ]
    });

    /// <----- []
    print("patch->>${optionsFormArray.value}");

    optionForm.updateValue({
      'options': [
        {'name': "option #1", 'value': '#1 value'},
        {'name': "option #2", 'value': '#2 value'}
      ]
    });

    /// <----- [{name: option #1, value: #1 value}, {name: option #2, value: #2 value}]
    /// Get an ERROR: FormControlNotFoundException: control with name: '0.name' not found.
    print("update->>${optionsFormArray.value}");

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final colors = Theme.of(context).extension<MyColors>()!;

    return Scaffold(
        resizeToAvoidBottomInset: false,
        body: Container(
          color: colors.baseDarkBg,
          width: double.infinity,
          height: double.infinity,
          child: ReactiveForm(
            formGroup: optionForm,
            child: ReactiveFormArray(
              formArray: optionsFormArray,
              builder: (context, formArray, child) {
                return Column(
                  children: [
                    for (int i = 0; i < formArray.controls.length; i++)
                      Column(
                        children: [
                          ReactiveTextField(
                            formControlName: '$i.name',
                          ),
                          ReactiveTextField(
                            formControlName: '$i.value',
                          ),
                        ],
                      ),
                  ],
                );
              },
            ),
          ),
        ));
  }
}

@ShyshkovOleg ShyshkovOleg added the bug Something isn't working label May 14, 2024
@gibahjoe
Copy link

Hi, did you ever find a solution? Using formcontrol instead of formcontrolname didn't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants