<template slot-scope="scope"> <el-form-item :prop="'list.' + scope.$index + '.controlok'" :rules="fromDataRules.controlok" > <el-input v-if="scope.row.controluse === 'S2F21'" :disabled=" scope.row.iscontrol === false || scope.row.iscontrol == null || scope.row.iscontrol == '' " v-model="scope.row.controlok" filterable clearable /> <el-select v-else v-model="scope.row.controlok" :disabled=" scope.row.iscontrol === false || scope.row.iscontrol == null || scope.row.iscontrol == '' " :placeholder="t('web_ems_tip_pleaseInput')" filterable clearable > <el-option v-for="item in identifyingList" :key="item.id" :label="item" :value="item" /> </el-select> </el-form-item> </template>表单验证fromDataRules.controlok自定义若该行的iscontrol的值是true并且controlok的值为空时,提示请输入。代码怎么写
时间: 2024-02-10 12:04:08 浏览: 149
你可以在 `fromDataRules` 中添加一个自定义规则来实现该功能,代码如下:
```javascript
fromDataRules: {
controlok: [
{
required: true,
message: '请输入',
trigger: 'blur',
validator: (rule, value, callback) => {
let index = rule.field.split('.')[1];
let row = this.list[index];
if (row.iscontrol === true && (value === undefined || value.trim() === '')) {
callback(new Error('请输入'));
} else {
callback();
}
}
}
]
}
```
在该规则中,我们定义了一个自定义验证函数 `validator`,它会在输入框失去焦点时被调用。该函数会根据当前行的 `iscontrol` 值和 `controlok` 的值来判断是否需要进行验证。如果需要验证且 `controlok` 值为空,则会调用 `callback(new Error('请输入'))` 来提示用户输入。否则,调用 `callback()` 来表示验证通过。
在模板中,我们需要绑定 `fromDataRules.controlok` 规则,在 `el-form-item` 组件中添加 `prop` 属性,并将其值设为 `'list.' + scope.$index + '.controlok'`,这样该规则就会应用到该行的 `controlok` 属性上。
```html
<template slot-scope="scope">
<el-form-item
:prop="'list.' + scope.$index + '.controlok'"
:rules="fromDataRules.controlok">
<el-input
v-if="scope.row.controluse === 'S2F21'"
:disabled="scope.row.iscontrol === false || scope.row.iscontrol == null || scope.row.iscontrol == ''"
v-model="scope.row.controlok"
filterable
clearable />
<el-select
v-else
v-model="scope.row.controlok"
:disabled="scope.row.iscontrol === false || scope.row.iscontrol == null || scope.row.iscontrol == ''"
:placeholder="t('web_ems_tip_pleaseInput')"
filterable
clearable>
<el-option
v-for="item in identifyingList"
:key="item.id"
:label="item"
:value="item" />
</el-select>
</el-form-item>
</template>
```
阅读全文