Answer the question
In order to leave comments, you need to log in
To in Vue-dynamic form to organize the display of an error?
Good afternoon.
By design, the form has fields, has dynamic fields (those that the user can add / subtract). In case the user entered something incorrectly, the error is shown under the corresponding field.
Consider an example of an authorization form where the user is prompted to enter a login, password, and as many roles as desired:
<template>
<FForm @submit="doAuth" ref="authForm">
<FInputText caption="Имя пользователя" v-autofocus v-model="authForm.login"></FInputText>
<FInputText caption="Пароль" password v-model="authForm.password"></FInputText>
<div v-for="(_, index) in authForm.roles">
<FInputText
caption="Роль"
v-model="authForm.roles[index].name"
></FInputText>
</div>
<button @click="addRole">+</button>
<FButton caption="Войти" submit class="submit-button"></FButton>
</FForm>
</template>
<script>
export default {
name: "Test",
data() {
return {
authForm: {
roles: []
}
}
},
methods: {
addRole() {
this.authForm.roles.push({})
},
doAuth(e) {
// ...
}
},
}
</script>
{
"errors": {
"login": "Ошибка в логине",
"role[1].name": "Ошибка во второй роли"
}
}
Answer the question
In order to leave comments, you need to log in
Damn, wrote a detailed answer. Pressed preview and forgot. And then he returned, updated and ... everything was erased. 21st century, man.
I did that now you can do this:
<FForm @submit="doAuth" v-model="authForm">
<FInputText caption="Имя пользователя" v-autofocus f-name="login"></FInputText>
<FInputText caption="Пароль" password f-name="password"></FInputText>
<div v-array="roles">
<FInputText
caption="Роль"
f-name="name"
></FInputText>
</div>
<button @click="rolesAdd({})">+</button>
<FButton caption="Войти" submit class="submit-button"></FButton>
</FForm>
<FInputText
caption="Роль"
v-model="authForm.roles[index].name"
></FInputText>
<template v-if="hasRoleError(index)">
{{ errors.role[index].name }}
</template>
hasRoleError(index) {
return this.errors.role && this.errors.role[index];
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question