2019-11-06 18:40:18 +08:00
|
|
|
# Custom Rules
|
2020-02-05 12:14:05 +08:00
|
|
|
You can custom you valiation by setting custom trigger in rules.
|
2019-11-06 18:40:18 +08:00
|
|
|
```html
|
|
|
|
<n-form :model="model" ref="form" :rules="rules">
|
2019-11-11 13:35:44 +08:00
|
|
|
<n-form-item-row path="age" label="Age">
|
|
|
|
<n-input v-model="model.age"/>
|
|
|
|
</n-form-item-row>
|
|
|
|
<n-form-item-row path="password" label="Password">
|
|
|
|
<n-input v-model="model.password" @input="handlePasswordInput" type="password"/>
|
|
|
|
</n-form-item-row>
|
|
|
|
<n-form-item-row
|
2020-02-05 12:14:05 +08:00
|
|
|
first
|
2019-11-11 13:35:44 +08:00
|
|
|
path="reenteredPassword"
|
|
|
|
label="Re-enter Password"
|
|
|
|
ref="reenteredPassword"
|
|
|
|
>
|
|
|
|
<n-input :disabled="!model.password" v-model="model.reenteredPassword" type="password"/>
|
|
|
|
</n-form-item-row>
|
2020-03-09 09:29:48 +08:00
|
|
|
<n-form-item-row label="Env" path="env" rule-path="null">
|
2020-03-11 20:46:45 +08:00
|
|
|
<n-dynamic-input
|
2020-03-09 09:29:48 +08:00
|
|
|
v-model="model.env"
|
2020-03-11 16:24:35 +08:00
|
|
|
preset="pair"
|
2020-03-09 09:29:48 +08:00
|
|
|
/>
|
|
|
|
</n-form-item-row>
|
|
|
|
<n-form-item-row label="group" path="group" rule-path="null">
|
2020-03-11 20:46:45 +08:00
|
|
|
<n-dynamic-input
|
2020-03-09 09:29:48 +08:00
|
|
|
v-model="model.group"
|
2020-03-11 16:24:35 +08:00
|
|
|
preset='custom'
|
2020-03-09 09:29:48 +08:00
|
|
|
>
|
2020-03-11 16:24:35 +08:00
|
|
|
<template v-slot="slotProps">
|
|
|
|
<div style="width:100%">
|
|
|
|
<n-form-item
|
|
|
|
:path="'group[' + slotProps.index + '].inputNumberValue'"
|
|
|
|
rule-path="group.inputNumberValue"
|
|
|
|
>
|
|
|
|
<n-input-number v-model="slotProps.item.inputNumberValue"/>
|
|
|
|
</n-form-item>
|
|
|
|
<n-form-item
|
|
|
|
:path="'group[' + slotProps.index + '].input'"
|
|
|
|
rule-path="group.input"
|
|
|
|
>
|
|
|
|
<n-input v-model="slotProps.item.input"/>
|
|
|
|
</n-form-item>
|
|
|
|
</div>
|
|
|
|
</template>
|
2020-03-11 20:46:45 +08:00
|
|
|
</n-dynamic-input>
|
2020-03-09 09:29:48 +08:00
|
|
|
</n-form-item-row>
|
2019-11-06 18:40:18 +08:00
|
|
|
<n-row :gutter="[0, 24]">
|
|
|
|
<n-col :span="24">
|
|
|
|
<div style="display: flex; justify-content: flex-end;">
|
|
|
|
<n-button @click="handleValidateButtonClick" round type="primary">Validate</n-button>
|
|
|
|
</div>
|
|
|
|
</n-col>
|
|
|
|
</n-row>
|
|
|
|
</n-form>
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
{{ JSON.stringify(model, 0, 2) }}
|
|
|
|
</pre>
|
|
|
|
```
|
|
|
|
```js
|
|
|
|
export default {
|
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
model: {
|
|
|
|
age: null,
|
|
|
|
password: null,
|
2020-03-09 09:29:48 +08:00
|
|
|
reenteredPassword: null,
|
|
|
|
env: [
|
|
|
|
{
|
|
|
|
key:'',
|
|
|
|
value:''
|
|
|
|
}
|
|
|
|
],
|
|
|
|
group: [
|
|
|
|
{
|
2020-03-11 16:24:35 +08:00
|
|
|
inputNumberValue: 1,
|
2020-03-09 12:12:31 +08:00
|
|
|
input: null
|
2020-03-09 09:29:48 +08:00
|
|
|
}
|
|
|
|
]
|
2019-11-06 18:40:18 +08:00
|
|
|
},
|
|
|
|
rules: {
|
|
|
|
age: [
|
|
|
|
{
|
2019-11-11 13:35:44 +08:00
|
|
|
required: true,
|
2019-11-06 18:40:18 +08:00
|
|
|
validator (rule, value) {
|
2019-11-11 13:35:44 +08:00
|
|
|
if (!value) {
|
|
|
|
return new Error('Age is required')
|
|
|
|
} else if (!/^\d*$/.test(value)) {
|
|
|
|
return new Error('Age should be an integer')
|
|
|
|
} else if (Number(value) < 18) {
|
|
|
|
return new Error('Age should be above 18')
|
|
|
|
}
|
|
|
|
return true
|
2019-11-06 18:40:18 +08:00
|
|
|
},
|
2019-11-11 13:35:44 +08:00
|
|
|
trigger: ['input', 'blur']
|
2019-11-06 18:40:18 +08:00
|
|
|
}
|
|
|
|
],
|
|
|
|
reenteredPassword: [
|
2020-02-05 12:14:05 +08:00
|
|
|
{
|
|
|
|
required: true,
|
|
|
|
message: 'Re-entered Password is required',
|
|
|
|
trigger: ['input', 'blur']
|
|
|
|
},
|
2019-11-06 18:40:18 +08:00
|
|
|
{
|
|
|
|
validator: this.validatePasswordStartWith,
|
|
|
|
message: 'Password is not same as re-entered password!',
|
|
|
|
trigger: 'input'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
validator: this.validatePasswordSame,
|
|
|
|
message: 'Password is not same as re-entered password!',
|
2019-11-07 21:35:26 +08:00
|
|
|
trigger: ['blur', 'password-input']
|
2019-11-06 18:40:18 +08:00
|
|
|
}
|
2020-03-09 09:29:48 +08:00
|
|
|
],
|
|
|
|
env: {
|
|
|
|
key: {
|
|
|
|
required: true,
|
|
|
|
message: 'Please input your key',
|
|
|
|
trigger: ['input', 'blur']
|
|
|
|
},
|
|
|
|
value: {
|
|
|
|
required: true,
|
|
|
|
message: 'Please input your value',
|
|
|
|
trigger: ['input', 'blur']
|
|
|
|
}
|
|
|
|
},
|
|
|
|
group: {
|
|
|
|
inputNumberValue: {
|
|
|
|
validator: this.validateGroupNumber,
|
|
|
|
trigger: ['blur', 'change'],
|
|
|
|
message: 'Please input a number which is not zero'
|
|
|
|
},
|
2020-03-09 12:12:31 +08:00
|
|
|
input: {
|
2020-03-09 09:29:48 +08:00
|
|
|
required: true,
|
|
|
|
message: 'Please input your key',
|
|
|
|
trigger: ['input', 'blur']
|
|
|
|
},
|
|
|
|
}
|
2019-11-06 18:40:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
handlePasswordInput () {
|
2019-11-07 21:35:26 +08:00
|
|
|
if (this.model.reenteredPassword) {
|
2020-02-05 12:48:15 +08:00
|
|
|
this.$refs.reenteredPassword.validate('password-input')
|
2019-11-07 21:35:26 +08:00
|
|
|
}
|
2019-11-06 18:40:18 +08:00
|
|
|
},
|
|
|
|
handleValidateButtonClick (e) {
|
|
|
|
e.preventDefault()
|
2019-11-28 11:05:54 +08:00
|
|
|
this.$refs.form.validate(errors => {
|
|
|
|
if (!errors) {
|
|
|
|
this.$NMessage.success('Valid')
|
2019-11-27 17:23:24 +08:00
|
|
|
} else {
|
2019-11-28 11:05:54 +08:00
|
|
|
console.log(errors)
|
|
|
|
this.$NMessage.error('Invalid')
|
2019-11-27 17:23:24 +08:00
|
|
|
}
|
|
|
|
})
|
2019-11-06 18:40:18 +08:00
|
|
|
},
|
|
|
|
validatePasswordStartWith (rule, value) {
|
|
|
|
return this.model.password && this.model.password.startsWith(value) && this.model.password.length >= value.length
|
|
|
|
},
|
|
|
|
validatePasswordSame (rule, value) {
|
|
|
|
return value === this.model.password
|
2020-03-09 09:29:48 +08:00
|
|
|
},
|
|
|
|
validateGroupNumber (rule, value) {
|
|
|
|
return value !== 0
|
2019-11-06 18:40:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|