Vue form error fix
March 21, 2025 · AI Conversations
<script>
import { ref } from 'vue'
import { useAuthStore } from '@/stores/auth'
const form = ref(null)
const items = [
'Item 1',
'Item 2',
'Item 3',
'Item 4',
]
const title = ref('')
const description = ref('') // will contain HTML
const descriptionRules = ref([
(v: string) => !!v || 'Description is required',
])
const url = ref('')
const imageFile = ref(null)
const nameRules = ref([
(v: string) => !!v || 'Name is required',
(v: string) => (v && v.length <= 10) || 'Name must be 10 characters or less',
])
const urlRules = ref([
(v: string) => !!v || 'URL is required',
(v: string) => (v && v.length <= 10) || 'URL must be 10 characters or less',
])
const imageRules = ref([
(v: File) => !!v || 'Image is required',
(v: File) => v && v.size <= 1024 * 1024 || 'Image must be less than 1 MB',
])
const select = ref(null)
const checkbox = ref(false)
const submit = async () => {
}
</script>
Add Portfolio
Reset Form
AddPortfolio.vue?t=1742540937883:167 TypeError: Cannot read properties of undefined (reading '_withMods')
at AddPortfolio.vue:1:1
TypeError: Cannot read properties of undefined (reading '_withMods')
usually points to a **rendering issue**, often triggered when Vue tries to compile a component that has a syntax error or an improper usage of directives, especially with setup components or “ context. However, in your case, you're using a **normal “** and binding everything correctly.
Let’s troubleshoot and improve this:
—
### ✅ 1. **Check v-models**
You're using `ref()` for `title`, `url`, etc., and binding them to `v-model`, which is fine.
But you have a **rich text editor placeholder** in the comment:
<!-- Rich Text Editor -->
If you're trying to use something like “ or a third-party rich text editor like `Quill`, but haven't actually added the component, Vue will throw an error while rendering.
—
### ✅ 2. **Fix `ref="form"` Binding**
You have:
const form = ref(null)
But in “, Vue doesn't automatically connect it unless you're using the **Composition API template ref binding** like this:
<v-form ref="formRef" @submit.prevent="submit">
Then in “:
const formRef = ref()
So update:
<v-form ref="formRef" @submit.prevent="submit">
and
const formRef = ref()
—
### ✅ 3. **Check for Third-Party/Incomplete Imports**
You imported `useAuthStore`, but never used it. That’s fine as long as it's not breaking anything. But make sure it’s properly set up in `@/stores/auth`.
Also, if you’ve copied the template or script from somewhere and it has **remnants from “**, like `_withMods`, it might break. That error is a giveaway that Vue compiler might be expecting a setup-style component.
—
### ✅ 4. **Ensure All Variables Used in Template Are Returned**
Since you're not using “, you must **return** the refs and methods to make them available to the template:
export default {
setup() {
const formRef = ref(null)
const title = ref('')
const url = ref('')
const imageFile = ref(null)
const description = ref('')
const checkbox = ref(false)
const select = ref(null)
const nameRules = ref([
(v: string) => !!v || 'Name is required',
(v: string) => (v && v.length <= 10) || 'Name must be 10 characters or less',
])
const urlRules = ref([
(v: string) => !!v || 'URL is required',
(v: string) => (v && v.length <= 10) || 'URL must be 10 characters or less',
])
const imageRules = ref([
(v: File) => !!v || 'Image is required',
(v: File) => v && v.size <= 1024 * 1024 || 'Image must be less than 1 MB',
])
const submit = async () => {
console.log("Submitting", title.value, url.value)
}
const reset = () => {
title.value = ''
url.value = ''
imageFile.value = null
checkbox.value = false
select.value = null
}
return {
formRef,
title,
url,
imageFile,
description,
checkbox,
select,
nameRules,
urlRules,
imageRules,
submit,
reset
}
}
}
—
### ✅ 5. **Summary of Fixes**
– Return all refs and methods inside `setup()`.
– Use `formRef` instead of `form` unless `form` is returned correctly.
– Remove any incomplete/invalid components or placeholder code.
– Make sure all template-used variables are defined and returned.
– Ensure you're not mixing “ syntax with normal “ syntax.
—
Would you like me to convert this into full working code including a Vuetify rich text editor like `v-textarea` or integrating Quill as a rich text editor?