How to work with validations of nested components inside a parent component with Vuelidate
? I would like to change parentForm.$invalid
if inputs in subcomponents are valid or not.
Parent:
<parent-component>
</child-component-1>
</child-component-2>
</parent-component>
validations: {
parent: WHAT HERE?
}
Child-1
<child-component-1>
</some-input>
</child-component-1>
data() {
return {
someInput: ""
};
},
validations: {
someInput: required
}
Child-2
<child-component-2>
</some-input>
</child-component-2>
data() {
return {
someInput: ""
};
},
validations: {
someInput: required
}
The simplest way to get started with vuelidate for sub-components/form is to use Vue.js dependency injection mechanism provided by provide/inject
pair. The $v
instance created in parent component can be shared with children component.
As you more fine tune it, you can use Vuelidate data-nesting and only pass a subset of $v
to your subcomponents. This is a roughly similar approach to how Angular does with nested Forms. It would look something like:
export default {
data() {
return {
form1: {
nestedA: '',
nestedB: ''
} /* Remaining fields */
}
},
validations: {
form1: {
nestedA: {
required
},
nestedB: {
required
}
},
form2: {
nestedA: {
required
},
nestedB: {
required
}
}
}
}
Alternately, you can declare independent instances of $v
for each component. In your case, you will have one for parent and two for children. When you hit the submit button, get the reference of child component using $refs
and check if nested form within the child component is valid or not.