I have a form which contains name and address components. In the parent's page I have a submit button. I can send data from the parent to the children using props. Now I am trying to get the children's values from the parent's form. And I want to validate child fields from the parent's form. How to acheive this?
Here is my form structure.
parent.vue
<form @submit.prevent="handleSubmit">
<name-fields :user='user'></name-fields>
<address-fields :address='address'></address-fields>
<button>Register</button>
</form>
<script>
export default {
data () {
return {
user: {
firstName: 'Raja',
lastName: 'Roja',
},
address: {
doorNo: '11',
state: 'KL',
country: 'IN',
},
submitted: false
}
},
components: {
'name-fields': cNameFields,
'address-fields': cAddressFields,
},
}
</script>
cNameFields.vue
<template>
<div>
<div class="form-group">
<label for="firstName">First Name</label>
<input type="text" v-model="user.firstName" v-validate="'required'" name="firstName" class="form-control" :class="{ 'is-invalid': submitted && errors.has('firstName') }" />
<div v-if="submitted && errors.has('firstName')" class="invalid-feedback">{{ errors.first('firstName') }}</div>
</div>
<div class="form-group">
<label for="lastName">Last Name</label>
<input type="text" v-model="user.lastName" v-validate="'required'" name="lastName" class="form-control" :class="{ 'is-invalid': submitted && errors.has('lastName') }" />
<div v-if="submitted && errors.has('lastName')" class="invalid-feedback">{{ errors.first('lastName') }}</div>
</div>
</div>
</template>
<script>
export default {
name: 'name',
props: {
user: Object,
submitted : Boolean
},
</script>
Currently getting this output:
What I want to do: