1

First, I am wondering whether I am on the right track with the patterns I am using for my application. Is it correct for me to create a reusable BaseForm component?

Another way to pose my question is: How do I model the parent component's data dynamically based on the child components I have included using slots?

I simply want to re use the same submit handler for all my forms. The submit handler should use the action and method props to determine how to configure axios, and the request body will be dynamic based on the BaseInput and BaseSelect components that are slotted within the BaseForm component.

Here's my sandbox example:

https://codesandbox.io/s/brave-chaplygin-9e5u2?file=/src/App.vue

I am stuck on the BaseForm.vue's handleSubmit method.

I could always just serialize the form data using vanilla JavaScript, but I feel that I should be doing it the Vue way. I am welcome to any opinions and advice. I am new to this framework, so I could be getting it totally wrong.

1
  • 1
    This course is really nice and helped me understand a lot of cool reusable patterns: michaelnthiessen.com/reusable-components Not a promo, just a guy making nice content. Commented Feb 19, 2021 at 14:20

3 Answers 3

1

First of all you are passing event handlers to child components vie function props. But it's more common for React, not Vue.
In Vue you should just emit events and handle them in parent components. Read more about it here.
If you want to create reusable form component, one of the easiest ways is to do it with slots. You should create empty form with button (Cancel/Submit e.t.c) and add events for it.
Then you should just pass inputs to slot and that's all.
Working example:

https://codesandbox.io/s/charming-mclean-05jw7?file=/src/App.vue

Sign up to request clarification or add additional context in comments.

2 Comments

I agree that the slots feature in Vue holds the key, but your example doesn't solve my issue which is that I don't want to rewrite the submit handler logic for each form. You have the submit handler logic in the App.vue file, when it should be completely contained in the BaseForm.vue file
@devaent Which logic you don't want to copy, getting form data?
0

Thanks all for the replies. The answer as to how to do this is too complex to make it worth doing for my use case.

I still have reusable form input components, but having a reusable parent form component with a reusable form submit handler is not the way to go. Even though it would be nice to keep it DRY, I think the solution would be overengineered (if even possible).

Here’s my final result: https://codesandbox.io/s/romantic-almeida-wpxbw?file=/src/App.vue

Comments

0

I would just put a template ref on <form>, and create a FormData with it, which will include all the form's named <input>s in its default slot:

<form ref="form">...</form>
const formData = new FormData(this.$refs.form)

demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.