feat(Form): formatter add rule param

This commit is contained in:
陈嘉涵 2020-02-23 19:58:37 +08:00
parent d878354ebf
commit aba946f20e
4 changed files with 11 additions and 4 deletions

View File

@ -178,7 +178,7 @@ export default createComponent({
let value = this.formValue;
if (rule.formatter) {
value = rule.formatter(value);
value = rule.formatter(value, rule);
}
if (!this.runSyncRule(rule, value)) {

View File

@ -442,7 +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* |
| formatter `v2.5.3` | Format value before validate | *(value, rule) => any* |
### Events

View File

@ -481,7 +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* |
| formatter `v2.5.3` | 格式化函数,将表单项的值转换后进行校验 | *(value, rule) => any* |
### Events

View File

@ -71,7 +71,14 @@ test('rules prop - formatter', async () => {
data() {
return {
rules: [
{ required: true, formatter: val => val.trim(), message: 'foo' },
{
message: 'foo',
required: true,
formatter: (val, rule) => {
expect(rule.message).toEqual('foo');
return val.trim();
},
},
],
};
},