chore: 表单报错信息中增加所有表单项的报错信息 (#2357)

* chore: 表单报错信息中增加所有表单项的报错信息

* 修单元测试报错
This commit is contained in:
吴多益 2021-08-06 16:40:13 +08:00 committed by GitHub
parent 73ac02f803
commit 9144176c6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 6 deletions

View File

@ -60,7 +60,10 @@ test('Renderer:FormItem:validateApi:success', async () => {
expect(onSubmit).not.toHaveBeenCalled();
await wait(100);
expect(notify).toHaveBeenCalledWith('error', '依赖的部分字段没有通过验证');
expect(notify).toHaveBeenCalledWith(
'error',
'依赖的部分字段没有通过验证\na: 这是必填项'
);
const input = container.querySelector('input[name=a]');
expect(input).toBeTruthy();
@ -129,7 +132,10 @@ test('Renderer:FormItem:validateApi:failed', async () => {
expect(onSubmit).not.toHaveBeenCalled();
await wait(100);
expect(notify).toHaveBeenCalledWith('error', '依赖的部分字段没有通过验证');
expect(notify).toHaveBeenCalledWith(
'error',
'依赖的部分字段没有通过验证\na: 这是必填项'
);
const input = container.querySelector('input[name=a]');
expect(input).toBeTruthy();

View File

@ -106,7 +106,10 @@ test('Renderer:Form:valdiate', async () => {
expect(onSubmit).not.toHaveBeenCalled();
await wait(100);
expect(notify).toHaveBeenCalledWith('error', '依赖的部分字段没有通过验证');
expect(notify).toHaveBeenCalledWith(
'error',
'依赖的部分字段没有通过验证\na: 这是必填项'
);
const input = container.querySelector('input[name=a]');
expect(input).toBeTruthy();
@ -228,7 +231,10 @@ test('Renderer:Form:onValidate', async () => {
expect(onValidate.mock.calls[0][0]).toMatchSnapshot();
await wait(100);
expect(notify).toHaveBeenCalledWith('error', '依赖的部分字段没有通过验证');
expect(notify).toHaveBeenCalledWith(
'error',
'依赖的部分字段没有通过验证\na: a is wrong\nb: b is wrong\nb: b is wrong 2'
);
fireEvent.click(getByText('Submit'));
await wait(100);

View File

@ -459,9 +459,18 @@ export const FormStore = ServiceStore.named('FormStore')
)) ||
self.restError.length
) {
const msg = failedMessage ?? self.__('Form.validateFailed');
let msg = failedMessage ?? self.__('Form.validateFailed');
// 同时也列出所有表单项报错,方便在很长的表单中知道是哪个字段的问题
failedMessage ??
self.items.forEach(item => {
item.errorData.forEach(errorData => {
msg = `${msg}\n${item.name}: ${errorData.msg}`;
});
});
msg && getEnv(self).notify('error', msg);
throw new Error(self.__('Form.validateFailed'));
throw new Error(msg);
}
if (fn) {