How to de(activate) a button depending on the validity of an associated form? #2358
Replies: 4 comments 6 replies
-
I think there it not be possible yet (at least no straightforward way), unless you can hook every validation of the form. We need to new API to achieve this. I'm not sure what it should look like. |
Beta Was this translation helpful? Give feedback.
-
If you know any other library can do it, please leave some messages. |
Beta Was this translation helpful? Give feedback.
-
As a workaround, you can use the <script setup>
const isButtonValid = ref<boolean>(false)
const validateForm = () => {
formRef.value?.validate((errors) => {
if (!errors) {
console.log('Valid')
isButtonValid = true
} else {
console.log('Invalid', errors)
isButtonValid = false
}
})
}
const submit = () => {
// submitting....
}
</script>
<template>
<n-form @keydown="validateForm">
<n-form-item label="Name" path="name">
<n-input v-model:value="formValue.name" />
</n-form-item>
</n-from>
<n-button :disabled="isButtonValid" @click="submit">
Submit
</n-button>
</template> |
Beta Was this translation helpful? Give feedback.
-
I presume there has been no movement on this, which is disappointing as I intended to do exactly this. |
Beta Was this translation helpful? Give feedback.
-
Hi guys,
I want to activate a form's save button only if all fields are valid. So if the validation rules of at least one form element are not met, then the button should be disabled automatically.
How can I adjust the following minimal code sample to achieve this?
Codesandbox: https://codesandbox.io/s/naiveui-form-validation-1q4wc?file=/src/Demo.vue
I appreciate any help you can provide. :-)
Beta Was this translation helpful? Give feedback.
All reactions