Building a Dynamic Form in Vue Js with Laravel: Part 2

Posted by

Dynamic Form in Vue Js with Laravel | Part 2

Dynamic Form in Vue Js with Laravel | Part 2

In Part 1 of this series, we discussed how to create a dynamic form in Vue Js with Laravel. In this part, we will dive deeper into the process of building a dynamic form and explore some advanced features.

Dynamic Form with Vue Js

Vue Js is a popular JavaScript framework for building user interfaces. It provides a simple and declarative way to create dynamic web applications. In this part, we will continue building our dynamic form using Vue Js.

Dynamic Form Fields

One of the main features of a dynamic form is the ability to add and remove form fields dynamically. In Vue Js, we can achieve this by using v-for directive to loop through an array of form fields and v-model directive to bind form input values to data properties.


<div id="app">
<form @submit.prevent="submitForm">
<div v-for="(field, index) in formFields" :key="index">
<input type="text" v-model="field.value">
<button @click="removeField(index)">Remove</button>
</div>
<button @click="addField">Add Field</button>
<button type="submit">Submit</button>
</form>
</div>

<script>
new Vue({
el: '#app',
data: {
formFields: [
{ value: '' }
]
},
methods: {
addField() {
this.formFields.push({ value: '' });
},
removeField(index) {
this.formFields.splice(index, 1);
},
submitForm() {
// submit form data
}
}
});
</script>

In the above code, we have defined a form with an array of formFields. We use v-for directive to loop through the formFields array and v-model directive to bind form input values to the value property of each field object. We also have methods to add and remove form fields and to submit form data.

Validating Dynamic Form

Another important aspect of a dynamic form is form validation. In Vue Js, we can use the vee-validate library to add validation to our dynamic form. This library provides a set of validation rules and error messages that can be easily integrated into our Vue Js components.


<script>
import { ValidationProvider, extend } from 'vee-validate';
import { required } from 'vee-validate/dist/rules';

extend('required', {
...required,
message: 'This field is required'
});

new Vue({
el: '#app',
data: {
formFields: [
{ value: '' }
]
},
methods: {
addField() {
this.formFields.push({ value: '' });
},
removeField(index) {
this.formFields.splice(index, 1);
},
submitForm() {
this.$validator.validateAll().then((result) => {
if (result) {
// submit form data
}
});
}
},
components: {
ValidationProvider
}
});
</script>

In the above code, we have imported the ValidationProvider component from the vee-validate library and added required validation to our form fields. We also use the $validator object to validate the form before submitting the data.

Conclusion

In this part, we continued building our dynamic form in Vue Js with Laravel and explored some advanced features such as adding and removing form fields dynamically, and form validation. In the next part, we will further enhance our dynamic form by adding file upload and form submission using Laravel.