Vee-validate: [V4] Nesting Form component

Created on 24 Sep 2020  路  9Comments  路  Source: logaretm/vee-validate

Versions

  • vee-validate: 4.0.0-alpha.14
  • vue: 3.0.0

Describe
In older version I could nest Validation Observer to show the error in different places, w/ V4 I try to do that but only the nearest Form component react w/ error.
Is there any way to do that with the Form component?

Most helpful comment

I will try to build an example in the docs.

All 9 comments

Due to too many issues caused by ValidatiobObserver being nestable, I'm not doing nested forms in v4. And to be honest I never found a really good use-case for them.

However, I think you can build your own with useForm.

@logaretm thank you for replying. I'll try to use useForm.

My use case is:
Root form (with submit button enable if all field in all section are valid)
|--Section 1 (with green mark icon when all the field are valid)
|-- Section 2 ...
|--...

I see, forgive me if I sounded rude. It would take manual labor to get it to work. For example you need to create a parent form for them that uses inject to be able to hook into them later, pretty much what happens with useField and useForm, so you will need to create a wrapper that does the same for useForm to aggregate their results.

So something along these lines:

// useFormAggregator.js

export function useFormAggregator() {
  const forms = ref([]);

  // will be used by forms to add them to the forms array
  function registerForm(form) {
     forms.value.push(form);
  }

  provide('my_form_aggregator', registerForm);

  const mergedFlags = computed(() => {
     // iterate and reduce the `forms` meta flags objects
  });


  const mergedErrors = computed(() => {
     // iterate and merge the forms error objects
  });

  return {
    mergedFlags,
    mergedErrors
  };
}

And now in your custom form components, you can use useForm:

// in setup
const form = useForm();
const aggregator = inject('my_form_aggregator');

// adds the form to the aggregator
aggregator(form);

that means the nearest component that will call useFormAggregator in its setup will be able to aggregate the child forms state. You can use Field component or useField as both works regardless of how forms are created.

This can be actually an example I would add to the docs since I think this feature will be asked a lot.

@logaretm 馃槏 many thank, this will help me a lot

Can't wait until the V4 is official released

@logaretm I was successful to aggregate the form, but one issue happens is only the errors reactive, the meta is not reactive

That's really cool, the meta might need to be converted to refs because Vue 3 recently doesn't unwrap nested reactive objects, maybe try toRefs?

@logaretm unfortunately, nothing works, I try to wrap with ref, toRefs, reactive. The value of meta is never reactive with the change.

I will try to build an example in the docs.

That would be really nice, thank you in advance.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Etheryte picture Etheryte  路  3Comments

schel4ok picture schel4ok  路  3Comments

HunterJS-bit picture HunterJS-bit  路  3Comments

7immer picture 7immer  路  3Comments

yyyuuu777 picture yyyuuu777  路  3Comments