Vue 2.6 + Composition API + Vuelidate
See original GitHub issueHey, I’m trying to use the next branch with Vue 2.6 and Composition API (in quasar framework), but it doesn’t work, I got the following error :
TypeError: Object(...) is not a function
at useVuelidate (index.esm.js?dc83:580)
at useRegisterForm (use-register-form.ts?f336:57)
Quasar Framework is not yet shipped with Vue 3, so I tried the old version of Vuelidate, I export the validations object in the setup function but I think it has no effect as I can’t find the $v object.
import { reactive, toRefs, defineComponent } from '@vue/composition-api'
import { api } from 'boot/axios'
import {
required,
email,
minLength,
sameAs
} from 'vuelidate/lib/validators'
type Selected = {
label: string,
value: string | number
}
export default function useRegisterForm () {
const state = reactive({
lastName: '',
firstName: '',
email: '',
address: '',
password: '',
passwordConfirmation: '',
terms: false,
dialCode: 0,
zipCode: null,
mobile: '',
countries: [] as Array<Selected>,
country: {
label: null,
value: null,
iso: null,
dial_code: null
},
states: [] as Array<Selected>,
state: {
label: null,
value: null
},
cities: [] as Array<Selected>,
city: {
label: null,
value: null
}
})
const validations = {
email: {
required,
email
}
}
// METHODS
async function onSubmit (): Promise<void> {
try {
const { data } = await api.post<Array<Record<string, any>>>('/register', {
last_name: state.lastName,
first_name: state.firstName,
email: state.email,
mobile: `+${state.dialCode}${state.mobile.replace(/\s/g, '')}`,
country: state.country.value,
state: state.state.value,
city: state.city.value,
address: state.address,
zip_code: state.zipCode,
password: state.password,
password_confirmation: state.passwordConfirmation,
terms: state.terms
})
} catch (e) {
}
}
return {
state,
onSubmit,
validations
}
}
export default defineComponent({
name: 'Register',
setup (props, context) {
const { state, onSubmit, validations } = useRegisterForm()
return {
...toRefs(state as any),
validations
}
}
})
I have no JS error. Any idea please?
Issue Analytics
- State:
- Created 3 years ago
- Comments:22 (8 by maintainers)
Top Results From Across the Web
Vuelidate using Vue 2.6 and Composition API - Stack Overflow
I'm using the Composition API together with Vue 2 (by using @vue/composition-api ) combined with the following two libraries ...
Read more >Vuelidate 2 - Composition API + Vue 3 - CodeSandbox
CodeSandbox is an online editor tailored for web applications.
Read more >Vuelidate: Getting started
Vuelidate 2 is a simple, but powerful, lightweight model-based validation ... When used with Vue 2.x, you need to install the @vue/composition-api plugin....
Read more >[Solved]-Vuelidate using Vue 2.6 and Composition API-Vue.js
Vuelidate using Vue 2.6 and Composition API · Strongly typing props of vue components using composition api and typescript typing system · What...
Read more >Easy Form Validation With Vuelidate | Vue 3 - YouTube
Join My Weekly Dev Newsletter: https://www.johnkomarnicki.com/#newsletterForm validation is an essential part of any form, and you want to ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi @Melchyore, yes i see. A workaround in this case could be something like this:
Register.vue
use-register-form.js
Im using vue 2.6 with composition api like @alexdee2007 suggested above, it works great for simple validation, but I’m struggling to get variables or functions from the setup inside the validation section for example
If I get the current Instance instance inside the validation section I could do this, but is not updating the values after any input change