perf: setValues method of the form supports assigning values only to keys that exist in the schema (#4508)

* fix: hover border style same as antd style when validate error

* fix: hover border style same as antd style when validate error

* feat(@vben-core/form-ui): Default form validation rules applicable to selector components

* fix: Missing the default required label style for components such as select

* fix: the focus style and antd of the input box validation failure should be consistent

* fix: the focus style and antd of the input box validation failure should be consistent

* fix: some antd components failed to verify styles

* perf: setValues method of the form supports assigning values only to keys that exist in the schema

* docs: update form component docs

---------

Co-authored-by: vince <vince292007@gmail.com>
pull/48/MERGE
LinaBell 2024-09-25 17:09:38 +08:00 committed by GitHub
parent abbbbfb955
commit bb6057cac3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 2 deletions

View File

@ -229,7 +229,7 @@ useVbenForm 返回的第二个参数,是一个对象,包含了一些表单
| --- | --- | --- |
| submitForm | 提交表单 | `(e:Event)=>Promise<Record<string,any>>` |
| resetForm | 重置表单 | `()=>Promise<void>` |
| setValues | 设置表单值 | `()=>Promise<Record<string,any>>` |
| setValues | 设置表单值, 默认会过滤不在schema中定义的field, 可通过filterFields形参关闭过滤 | `(fields: Record<string, any>, filterFields?: boolean, shouldValidate?: boolean) => Promise<void>` |
| getValues | 获取表单值 | `(fields:Record<string, any>,shouldValidate: boolean = false)=>Promise<void>` |
| validate | 表单校验 | `()=>Promise<void>` |
| resetValidate | 重置表单校验 | `()=>Promise<void>` |

View File

@ -17,6 +17,8 @@ import {
StateHandler,
} from '@vben-core/shared/utils';
import { objectPick } from '@vueuse/core';
const merge = createMerge((originObj, key, updates) => {
if (Array.isArray(originObj[key]) && Array.isArray(updates)) {
originObj[key] = updates;
@ -182,12 +184,25 @@ export class FormApi {
}
}
/**
*
* @param fields record
* @param filterFields schema true
* @param shouldValidate
*/
async setValues(
fields: Record<string, any>,
filterFields: boolean = true,
shouldValidate: boolean = false,
) {
const form = await this.getForm();
form.setValues(fields, shouldValidate);
if (!filterFields) {
form.setValues(fields, shouldValidate);
return;
}
const fieldNames = this.state?.schema?.map((item) => item.fieldName) ?? [];
const filteredFields = objectPick(fields, fieldNames);
form.setValues(filteredFields, shouldValidate);
}
async submitForm(e?: Event) {