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

feat nested_form_builder.dart add NestedFormBuilder; #1156

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Hu-Wentao
Copy link
Contributor

@Hu-Wentao Hu-Wentao commented Nov 25, 2022

Add NestedFormBuilder and some example code

resolve issue #239 and #1117

{
  "foo": "foo value0",
  "inner": {
    "bar": "bar value1",
    "baz": "baz value2",
  }
}
   FormBuilder(
            key: formKey,
            initialValue: const {
              'foo': 'foo value0',
              'inner': {
                'bar': 'bar value1',
                'baz': 'baz value2',
              }
            },
            child: Column(
              children: [
                FormBuilderTextField(name: 'foo'),
                Padding(
                  padding: const EdgeInsets.only(left: 16.0),
                  child: NestedFormBuilder(
                      name: 'inner',
                      child: Column(
                        children: [
                          FormBuilderTextField(name: 'bar'),
                          FormBuilderTextField(name: 'baz'),
                        ],
                      )),
                )
              ],
            )),

feat nested_form.dart NestedForm add NestedFormBuilder Demo
@codecov
Copy link

codecov bot commented Nov 26, 2022

Codecov Report

Attention: Patch coverage is 0% with 23 lines in your changes are missing coverage. Please review.

Project coverage is 82.22%. Comparing base (76be651) to head (e4d1d45).
Report is 219 commits behind head on main.

Files Patch % Lines
lib/src/nested_form_builder.dart 0.00% 23 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1156      +/-   ##
==========================================
- Coverage   84.93%   82.22%   -2.72%     
==========================================
  Files          19       20       +1     
  Lines         697      720      +23     
==========================================
  Hits          592      592              
- Misses        105      128      +23     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ozayr
Copy link

ozayr commented Nov 26, 2022

I just came to issues looking for a way to nest forms, appreciate this!

Copy link
Collaborator

@deandreamatias deandreamatias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 more things to review:

  • Revert pubspec.lock changes
  • Add tests (can be minimal) to NestedFromBuilder
  • Add how use nested builder to README.md

lib/src/nested_form_builder.dart Show resolved Hide resolved
example/lib/main.dart Show resolved Hide resolved
FormBuilderLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: FormBuilderLocalizations.delegate.supportedLocales,
home: const CompleteForm(),
// supportedLocales: FormBuilderLocalizations.delegate.supportedLocales,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please replace this line with language config, like this:

      supportedLocales: [
        Locale('de'),
        Locale('en'),
        Locale('es'),
        Locale('fr'),
        Locale('it'),
        ...
      ],

@deandreamatias
Copy link
Collaborator

Thanks a lot for this contribution! Will be a big step foward to improve this package.
Because this is a new important feature, required some changes to can be stable and easy to use for all

@github-actions github-actions bot added the Stale label Dec 29, 2022
@k-ane
Copy link

k-ane commented Jan 7, 2023

Hi, I have been testing this new feature (it's awesome btw, thanks for the PR) and I was wondering how validation will work? For me right now, validating the parent/main form didn't trigger validation on the nested/child fields.

@k-ane
Copy link

k-ane commented Jan 7, 2023

I got the nested form validation to work for me by adding a validator to the FormBuilderField of NestedFormBuilder, like so:

@override
  Widget build(BuildContext context) => FormBuilderField<Map<String, dynamic>>(
        name: name,
        initialValue:
            parentFormKey?.currentState?.initialValue[name] ?? initialValue,
        valueTransformer: valueTransformer ?? (_) => _,
        onReset: () => formKey.currentState?.reset(),
        // Added validator here
        validator: (_) =>
            (formKey.currentState?.validate() ?? false) ? null : '',
        builder: (field) => FormBuilder(
          key: formKey,
          initialValue: field.value ?? {},
          onChanged: () {
            final st = formKey.currentState;
            if (st == null) return;
            st.save();
            field.didChange(valueTransformer?.call(st.value) ?? st.value);
          },
          autovalidateMode: autovalidateMode,
          onWillPop: onWillPop,
          skipDisabled: skipDisabled,
          enabled: enabled,
          autoFocusOnValidationFailure: autoFocusOnValidationFailure,
          clearValueOnUnregister: clearValueOnUnregister,
          child: child,
        ),
      );

@k-ane
Copy link

k-ane commented Jan 8, 2023

Something else worth noting, I had to add

autovalidateMode: autovalidateMode,

just below the new validator, otherwise the nested form was validating on every change. Previously the autovalidateMode was only being passed to the FormBuilder and not the FormBuilderField component

@github-actions github-actions bot removed the Stale label Jan 8, 2023
@mfizz1
Copy link

mfizz1 commented Aug 25, 2023

Any update on this? This is such a game changer that is desperately needed.

@github-actions github-actions bot removed the Stale label Aug 26, 2023
@github-actions github-actions bot added the Stale label Sep 25, 2023
@letto4135
Copy link

Ready for nested forms 👍

@github-actions github-actions bot removed the Stale label Dec 11, 2023
@github-actions github-actions bot added the Stale label Jan 10, 2024
@Arpit1496
Copy link

any update on this ?

@github-actions github-actions bot removed the Stale label Jan 31, 2024
@github-actions github-actions bot added Stale and removed Stale labels Mar 1, 2024
@github-actions github-actions bot added the Stale label Apr 1, 2024
@UsamaKarim
Copy link

It doesn't seem to solve all use cases, for example, if I have a nested list,

{
  "foo": "foo value0",
  "inner": [
    {
      "bar": "bar value1",
      "baz": "baz value2"
    },
    {
      "bar": "bar value1",
      "baz": "baz value2"
    }
  ]
}

@github-actions github-actions bot removed the Stale label Apr 20, 2024
@github-actions github-actions bot added the Stale label May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants