提交表单校验失败事件优化

This commit is contained in:
pianruijie 2022-02-18 15:02:41 +08:00
parent 1eb4321151
commit a00614ae0b
3 changed files with 92 additions and 17 deletions

View File

@ -86,6 +86,7 @@ export default {
{ {
name: 'form-action-receiver', name: 'form-action-receiver',
id: 'form-action-receiver', id: 'form-action-receiver',
title: "表单用于接收上面按钮的动作派发form本身的事件",
type: 'form', type: 'form',
debug: true, debug: true,
api: '/api/mock2/form/saveForm', api: '/api/mock2/form/saveForm',
@ -174,7 +175,7 @@ export default {
body: [ body: [
{ {
type: 'tpl', type: 'tpl',
tpl: '当前表单数据:${val|json}' tpl: '当前表单数据:${val|json}'
} }
] ]
} }
@ -196,7 +197,7 @@ export default {
body: [ body: [
{ {
type: 'tpl', type: 'tpl',
tpl: '当前表单数据:${val|json}' tpl: '当前表单数据:${val|json}'
} }
] ]
} }
@ -277,7 +278,7 @@ export default {
val: '${event.data}' val: '${event.data}'
}, },
dialog: { dialog: {
title: '触发表单校验失败事件', title: '触发表单提交失败事件',
data: { data: {
val: '${val}' val: '${val}'
}, },
@ -292,6 +293,83 @@ export default {
] ]
} }
} }
},
{
type: 'form',
debug: true,
api: '/api/mock2/form/saveForm',
title: "表单派发formItem的校验事件",
data: {
data1: '初始化数据1',
data2: '初始化数据2'
},
initApi:
'https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/mock2/form/initData',
body: [
{
type: 'input-text',
name: 'name',
label: '姓名',
required: true,
validateOnChange: true
},
{
type: 'input-text',
name: 'email',
label: '邮箱',
required: true,
validateOnChange: true,
validations: {
isEmail: true
}
}
],
onEvent: {
formItemValidateSucc: {
actions: [
{
actionType: 'dialog',
args: {
val: '${event.data}'
},
dialog: {
title: '触发表单项校验成功事件',
data: {
val: '${val}'
},
body: [
{
type: 'tpl',
tpl: '当前表单数据:${val|json}'
}
]
}
}
]
},
formItemValidateError: {
actions: [
{
actionType: 'dialog',
args: {
val: '${event.data}'
},
dialog: {
title: '触发表单项校验失败事件',
data: {
val: '${val}'
},
body: [
{
type: 'tpl',
tpl: '当前表单数据:${val|json}'
}
]
}
}
]
}
}
} }
] ]
}; };

View File

@ -785,14 +785,15 @@ export default class Form extends React.Component<FormProps, object> {
submit(fn?: (values: object) => Promise<any>): Promise<any> { submit(fn?: (values: object) => Promise<any>): Promise<any> {
const {store, messages, translate: __, dispatchEvent, data} = this.props; const {store, messages, translate: __, dispatchEvent, data} = this.props;
this.flush(); this.flush();
const validateErrCb = () => {
dispatchEvent('validateFail', createObject(data));
};
return store.submit( return store.submit(
fn, fn,
this.hooks['validate'] || [], this.hooks['validate'] || [],
__(messages && messages.validateFailed) __(messages && messages.validateFailed),
).catch(error => { validateErrCb,
dispatchEvent('validateFail', createObject(data)); );
});
} }
// 如果开启了 lazyChange需要一个 flush 方法把队列中值应用上。 // 如果开启了 lazyChange需要一个 flush 方法把队列中值应用上。
@ -861,10 +862,6 @@ export default class Form extends React.Component<FormProps, object> {
} }
} }
// 问题1sumbit的时候校验成功事件和提交成功事件的顺序问题 已解决
// 问题2itme开启validateOnChange的时候如何触发事件
// 问题3提交到target时候是否需要触发事件ajax请求里面、drawconfirm里面应该都不需要吧 需要确认
// 问题4确定props.data是如何同步到store.data里面的
emitChange(submit: boolean) { emitChange(submit: boolean) {
const {onChange, store, submitOnChange, dispatchEvent, data} = this.props; const {onChange, store, submitOnChange, dispatchEvent, data} = this.props;
@ -985,7 +982,6 @@ export default class Form extends React.Component<FormProps, object> {
} }
}); });
} }
debugger
if ( if (
action.type === 'submit' || action.type === 'submit' ||
action.actionType === 'submit' || action.actionType === 'submit' ||
@ -1702,7 +1698,6 @@ export class FormRenderer extends Form {
// if (this.props.disabled) { // if (this.props.disabled) {
// return; // return;
// } // }
console.log(action);
if (action.target && action.actionType !== 'reload') { if (action.target && action.actionType !== 'reload') {
const scoped = this.context as IScopedContext; const scoped = this.context as IScopedContext;

View File

@ -455,11 +455,13 @@ export const FormStore = ServiceStore.named('FormStore')
const submit: ( const submit: (
fn?: (values: object) => Promise<any>, fn?: (values: object) => Promise<any>,
hooks?: Array<() => Promise<any>>, hooks?: Array<() => Promise<any>>,
failedMessage?: string failedMessage?: string,
validateErrCb: () => void
) => Promise<any> = flow(function* submit( ) => Promise<any> = flow(function* submit(
fn: any, fn: any,
hooks?: Array<() => Promise<any>>, hooks?: Array<() => Promise<any>>,
failedMessage?: string failedMessage?: string,
validateErrCb: () => void
) { ) {
self.submited = true; self.submited = true;
self.submiting = true; self.submiting = true;
@ -479,7 +481,7 @@ export const FormStore = ServiceStore.named('FormStore')
const env = getEnv(self); const env = getEnv(self);
msg && env.notify('error', msg); msg && env.notify('error', msg);
validateErrCb && validateErrCb();
throw new Error(msg); throw new Error(msg);
} }