First of all, you don't need both error
and error-messages
prop.
(首先,您不需要error
和error-messages
属性。)
If you just set error-messages
, the input field will go in error state and the message will be shown(如果仅设置error-messages
,则输入字段将进入错误状态,并且将显示该消息)
Do I need to specifically create a variable in the data() object in order to do what I wanna do?
(为了执行我想做的事情,是否需要在data()对象中专门创建一个变量?)
because if I have to do it that way it'd mean that I need to create an error and an error-messages properties in the data object for each and every field in my form!(因为如果我必须那样做,那意味着我需要为表单中每个字段的数据对象创建一个错误和一个错误消息属性!)
Yes, you'll need to set error-messages
prop for each and every field.
(是的,您需要为每个字段设置error-messages
属性。)
You already have separate variables for v-model
and rules
.(您已经具有用于v-model
和rules
单独变量。)
Ideally you would be running a for loop to generate the input fields(shown below), but a simple :error-messages="errorMessages.code"
& :error-messages="errorMessages.name"
will also work.
(理想情况下,您将运行for循环以生成输入字段(如下所示),但是简单的:error-messages="errorMessages.code"
& :error-messages="errorMessages.name"
也将起作用。)
// Fields array
[
{
name: 'code',
rules: [ // list of rules ],
value: '' // Some value,
errorMessages: [] // Could be list or string
},
{
name: 'name',
rules: [ // list of rules ],
value: '' // Some value,
errorMessages: [] // Could be list or string
}
]
// Your form template
<v-row v-for="field in fields" :key="field.name">
<v-col cols="12" class="d-flex">
<v-text-field
clearable
outlined
required
:rules="field.rules"
:name="field.name"
v-model="field.value"
:label="field.name"
:error-messages="field.errorMessages"
>
</v-text-field>
</v-col>
</v-row>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…