feat(Form): support rule formatter

This commit is contained in:
陈嘉涵
2020-02-23 19:56:44 +08:00
parent e48e5d47cd
commit d878354ebf
4 changed files with 52 additions and 17 deletions
+1
View File
@@ -442,6 +442,7 @@ export default {
| validator `v2.5.3` | Custom validator | *(value, rule) => boolean \| Promise* |
| pattern `v2.5.3` | Regex pattern | *RegExp* |
| trigger `v2.5.2` | When to validate the formcan be set to `onChange``onBlur` | *string* |
| formatter `v2.5.3` | Format value before validate | *(value) => any* |
### Events
+1
View File
@@ -481,6 +481,7 @@ export default {
| validator `v2.5.3` | 通过函数进行校验 | *(value, rule) => boolean \| Promise* |
| pattern `v2.5.3` | 通过正则表达式进行校验 | *RegExp* |
| trigger `v2.5.2` | 本项规则的触发时机,可选值为`onChange``onBlur` | *string* |
| formatter `v2.5.3` | 格式化函数,将表单项的值转换后进行校验 | *(value) => any* |
### Events
+29
View File
@@ -59,6 +59,35 @@ test('rules prop - pattern', async () => {
});
});
test('rules prop - formatter', async () => {
const onFailed = jest.fn();
const wrapper = mountForm({
template: `
<van-form @failed="onFailed">
<van-field name="A" :rules="rules" value=" " />
<van-button native-type="submit" />
</van-form>
`,
data() {
return {
rules: [
{ required: true, formatter: val => val.trim(), message: 'foo' },
],
};
},
methods: {
onFailed,
},
});
await submitForm(wrapper);
expect(onFailed).toHaveBeenCalledWith({
errors: [{ message: 'foo', name: 'A' }],
values: { A: ' ' },
});
});
test('rules prop - async validator', async () => {
const onFailed = jest.fn();
const wrapper = mountForm({