Reputation: 23
I am storing emails in an array called 'emailsDB' and trying to check if the email being entered by the user in the email input field exists in the array.
The exact steps that I have followed are provided here - https://www.npmjs.com/package/vee-validate
However, on implementation I get an error in the console which says -
SyntaxError: import declarations may only appear at top level of a module
HTML code
<div id="updateName" class="wrapper wrapper-right">
<div class="wrapper-inner full-height">
<!--Contact Sec-->
<section>
<!--form sec-->
<section class="animated container-fluid align-center sec-ptop">
<h3 class="salutation">
Hey <span v-if='formData.Name.length'>{{formData.Name}}</span> <span v-else>There</span>, happy to hear from you.
</h3>
<div>
<form v-on:submit.prevent="validateBeforeSubmit" name="contactform" method="post" class="row form-horizontal" role="form" novalidate="true">
<div class="form-group input--josh col-6">
<div class="input-wrap">
<input autocomplete="off" type="text" v-model="formData.Name" v-validate="'required'" id="Name" name="Name" placeholder="Name" class="form-control input__field input input__field--josh" :class="{'input': true, 'is-danger': errors.has('Name') }" />
<i v-show="errors.has('Name')" class="fa fa-exclamation"></i>
<label for="Name" class="input__label input__label input__label--josh input__label--josh-color-1 input__label--josh input__label--josh-color-1"></label>
</div>
<span v-show="errors.has('Name')" class="help is-danger">{{ errors.first('Name') }}</span>
</div>
<div class="form-group input--josh col-6">
<div class="input-wrap">
<input autocomplete="off" type="email" v-model="formData.Email" v-validate="'required|email'" id="Email" name="Email" placeholder="Email" class="form-control input input__field input__field--josh" :class="{'input': true, 'is-danger': errors.has('Email') }">
<i v-show="errors.has('Email')" class="fa fa-exclamation"></i>
<label for="Email" class="input__label input__label--josh input__label--josh-color-1"></label>
</div>
<span v-show="errors.has('Email')" class="help is-danger">{{ errors.first('Email') }}</span>
</div>
<div class="form-group input--josh col-12">
<div class="input-wrap">
<textarea rows="4" v-model="formData.Message" v-validate="'required'" id="Message" name="Message" placeholder="message" class="form-control input input__field input__field--josh" :class="{'input': true, 'is-danger': errors.has('Message') }"></textarea>
<i v-show="errors.has('Message')" class="fa fa-exclamation"></i>
<label for="Message" class="input__label input__label--josh input__label--josh-color-1"></label>
</div>
<span v-show="errors.has('Message')" class="help is-danger">{{ errors.first('Message') }}</span>
</div>
<div class="form-group col-12">
<div class="align-center">
<button type="submit" class="btn btn-default" data-ng-disabled="submitButtonDisabled">
<span class="mask"></span>
send
</button>
</div>
</div>
</form>
</div>
</section>
<!--/Contact Sec-->
</div>
</div>
Vue code
Vue.use(VeeValidate);
import { ValidationProvider } from 'vee-validate';
Vue.config.productionTip = false;
Vue.component('ValidationProvider', ValidationProvider);
const emailsDB = ["[email protected]"];
var app = new Vue({
el: '#updateName',
mounted() {
const isUnique = value =>
new Promise(resolve => {
setTimeout(() => {
if (emailsDB.indexOf(value) === -1) {
return resolve({
valid: true
});
}
return resolve({
valid: false,
data: {
message: `${value} has already reached out to me!`
}
});
}, 200);
});
Validator.extend("unique", {
validate: isUnique,
getMessage: (field, params, data) => data.message
});
}
})
Working example - https://codesandbox.io/s/y3504yr0l1?initialpath=%2F%23%2Fbackend&module=%2Fsrc%2Fcomponents%2FBackend.vue
Image of what the result should be: How the implementation should work
Upvotes: 2
Views: 3917
Reputation: 6788
The error import declarations may only appear at top level of a module means that when you use import
in a script, no other instruction may appear before.
So, you need to move the Vue.use(VeeValidate);
line after any import(s):
import { ValidationProvider } from 'vee-validate';
Vue.use(VeeValidate);
Vue.config.productionTip = false;
Vue.component('ValidationProvider', ValidationProvider);
const emailsDB = ["[email protected]"];
...
This has nothing to do with vee-validate
itself.
Upvotes: 1