I have a parent component rendering a personal details component and am injecting the parent's validator scope. if I use the v-validate directive and this.$validator.validateAll() or this.$validator.validate('field_name') this works fine.
However, I need to validate some fields independently, but when using this.$validator.attach('first_name', 'required') for example I am getting the following warning [vee-validate] A field is missing a "name" or "data-vv-name" attribute. I have tried moving the call to attach the validators into the click handler, in case the input element isn't fully rendered when calling it from mounted() but still get the same problem. I have also tried both name or data-vv-name attributes independently.
Parent.ts
import { Vue, Component } from 'vue-property-decorator';
import PersonalDetailsComponent from './PersonalDetails';
@Component({
template: `
<div class="container">
<personal-details-component></personal-details-component>
<div class="row">
<Button :onClick="handleButtonClick" :buttonText="'Validate'"></Button>
</div>
</div>
`,
components: {
PersonalDetailsComponent,
},
$_veeValidate: {validator: 'new'}
})
export default class ClaimComponent extends Vue {
mounted() {
this.attachValidators();
}
handleButtonClick() {
this.$validator.validateAll();
}
attachValidators() {
console.log(document.getElementsByName('first_name')); // Finds the element
this.$validator.attach('first_name', 'required');
this.$validator.attach('surname', 'required');
this.$validator.attach('email', 'required');
}
}
PersonalDetails.ts
import {Vue, Component, Inject} from 'vue-property-decorator';
import {Validator} from 'vee-validate';
@Component({
template: `
<div class="row">
<div class="col-12">
<form class="material-form">
<div class="group w-third">
<input v-model="first_name" type="text" name="first_name" required>
<span class="highlight"></span>
<span class="bar"></span>
<label>First name</label>
<span class="form-text error-text">{{ errors.first('first_name') }}</span>
</div>
<div class="group w-third">
<input v-model="surname" type="text" name="surname" required>
<span class="highlight"></span>
<span class="bar"></span>
<label>Surname</label>
<span class="form-text error-text">{{ errors.first('surname') }}</span>
</div>
<div class="group w-third">
<input v-model="email" type="text" name="email" required>
<span class="highlight"></span>
<span class="bar"></span>
<label>Email address</label>
<span class="form-text error-text">{{ errors.first('email') }}</span>
</div>
</form>
</div>
</div>
`
})
export default class PersonalDetailsComponent extends Vue {
@Inject('$validator') public $validator!: Validator;
first_name: string = '';
surname: string = '';
email: string = '';
}