mirror of
https://gitee.com/baidu/amis.git
synced 2024-12-02 03:58:07 +08:00
parent
e1af793147
commit
197682b2e6
@ -960,7 +960,12 @@ export default class Form extends React.Component<FormProps, object> {
|
||||
data = store.data;
|
||||
}
|
||||
if (Array.isArray(action.required) && action.required.length) {
|
||||
return store.validateFields(action.required).then(async result => {
|
||||
const fields = action.required.map(item => ({
|
||||
name: item,
|
||||
rules: {isRequired: true}
|
||||
}));
|
||||
|
||||
return store.validateFields(fields).then(async result => {
|
||||
if (!result) {
|
||||
const dispatcher = await dispatchEvent(
|
||||
'validateError',
|
||||
|
@ -17,6 +17,7 @@ import {
|
||||
} from '../utils/helper';
|
||||
import isEqual from 'lodash/isEqual';
|
||||
import flatten from 'lodash/flatten';
|
||||
import find from 'lodash/find';
|
||||
import {filter} from '../utils/tpl';
|
||||
import {normalizeApiResponseData} from '../utils/api';
|
||||
|
||||
@ -537,20 +538,27 @@ export const FormStore = ServiceStore.named('FormStore')
|
||||
return self.valid;
|
||||
});
|
||||
|
||||
const validateFields: (fields: Array<string>) => Promise<boolean> = flow(
|
||||
function* validateFields(fields: Array<string>) {
|
||||
const items = self.items.concat();
|
||||
let result: Array<boolean> = [];
|
||||
for (let i = 0, len = items.length; i < len; i++) {
|
||||
let item = items[i] as IFormItemStore;
|
||||
const validateFields: (
|
||||
fields: Array<string | {name: string; rules: {[propName: string]: any}}>
|
||||
) => Promise<boolean> = flow(function* validateFields(
|
||||
fields: Array<string | {name: string; rules: {[propName: string]: any}}>
|
||||
) {
|
||||
const items = self.items.concat();
|
||||
const normalizedfields = fields.map(field =>
|
||||
typeof field === 'string' ? {name: field, rules: {}} : field
|
||||
);
|
||||
let result: Array<boolean> = [];
|
||||
|
||||
if (~fields.indexOf(item.name)) {
|
||||
result.push(yield item.validate(self.data));
|
||||
}
|
||||
for (let i = 0, len = items.length; i < len; i++) {
|
||||
let item = items[i] as IFormItemStore;
|
||||
const field = find(normalizedfields, field => field.name === item.name);
|
||||
|
||||
if (field) {
|
||||
result.push(yield item.validate(self.data, undefined, field.rules));
|
||||
}
|
||||
return result.every(item => item);
|
||||
}
|
||||
);
|
||||
return result.every(item => item);
|
||||
});
|
||||
|
||||
function clearErrors() {
|
||||
const items = self.items.concat();
|
||||
|
@ -317,70 +317,83 @@ export const FormItemStore = StoreNode.named('FormItemStore')
|
||||
}
|
||||
|
||||
let validateCancel: Function | null = null;
|
||||
const validate: (data: Object, hook?: any) => Promise<boolean> = flow(
|
||||
function* validate(data: Object, hook?: any) {
|
||||
if (self.validating && !isEffectiveApi(self.validateApi, data)) {
|
||||
return self.valid;
|
||||
}
|
||||
|
||||
self.validating = true;
|
||||
clearError();
|
||||
if (hook) {
|
||||
yield hook();
|
||||
}
|
||||
|
||||
addError(
|
||||
doValidate(self.tmpValue, data, self.rules, self.messages, self.__)
|
||||
);
|
||||
|
||||
if (!self.errors.length && isEffectiveApi(self.validateApi, data)) {
|
||||
if (validateCancel) {
|
||||
validateCancel();
|
||||
validateCancel = null;
|
||||
}
|
||||
|
||||
const json: Payload = yield getEnv(self).fetcher(
|
||||
self.validateApi,
|
||||
/** 如果配置validateApi,需要将用户最新输入同步到数据域内 */
|
||||
createObject(data, {[self.name]: self.tmpValue}),
|
||||
{
|
||||
cancelExecutor: (executor: Function) =>
|
||||
(validateCancel = executor)
|
||||
}
|
||||
);
|
||||
validateCancel = null;
|
||||
|
||||
if (!json.ok && json.status === 422 && json.errors) {
|
||||
addError(
|
||||
String(
|
||||
json.errors || json.msg || `表单项「${self.name}」校验失败`
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
self.validated = true;
|
||||
|
||||
if (self.unique && self.form?.parentStore?.storeType === 'ComboStore') {
|
||||
const combo = self.form.parentStore as IComboStore;
|
||||
const group = combo.uniques.get(self.name) as IUniqueGroup;
|
||||
|
||||
if (
|
||||
group.items.some(
|
||||
item =>
|
||||
item !== self &&
|
||||
self.tmpValue !== undefined &&
|
||||
item.value === self.tmpValue
|
||||
)
|
||||
) {
|
||||
addError(self.__('Form.unique'));
|
||||
}
|
||||
}
|
||||
|
||||
self.validating = false;
|
||||
const validate: (
|
||||
data: Object,
|
||||
hook?: any,
|
||||
/**
|
||||
* customRules主要是为了支持action.require的验证方式
|
||||
* 这样可以基于不同的action实现不同的校验规则
|
||||
*/
|
||||
customRules?: {[propName: string]: any}
|
||||
) => Promise<boolean> = flow(function* validate(
|
||||
data: Object,
|
||||
hook?: any,
|
||||
customRules?: {[propName: string]: any}
|
||||
) {
|
||||
if (self.validating && !isEffectiveApi(self.validateApi, data)) {
|
||||
return self.valid;
|
||||
}
|
||||
);
|
||||
|
||||
self.validating = true;
|
||||
clearError();
|
||||
if (hook) {
|
||||
yield hook();
|
||||
}
|
||||
|
||||
addError(
|
||||
doValidate(
|
||||
self.tmpValue,
|
||||
data,
|
||||
customRules ? str2rules(customRules) : self.rules,
|
||||
self.messages,
|
||||
self.__
|
||||
)
|
||||
);
|
||||
|
||||
if (!self.errors.length && isEffectiveApi(self.validateApi, data)) {
|
||||
if (validateCancel) {
|
||||
validateCancel();
|
||||
validateCancel = null;
|
||||
}
|
||||
|
||||
const json: Payload = yield getEnv(self).fetcher(
|
||||
self.validateApi,
|
||||
/** 如果配置validateApi,需要将用户最新输入同步到数据域内 */
|
||||
createObject(data, {[self.name]: self.tmpValue}),
|
||||
{
|
||||
cancelExecutor: (executor: Function) => (validateCancel = executor)
|
||||
}
|
||||
);
|
||||
validateCancel = null;
|
||||
|
||||
if (!json.ok && json.status === 422 && json.errors) {
|
||||
addError(
|
||||
String(json.errors || json.msg || `表单项「${self.name}」校验失败`)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
self.validated = true;
|
||||
|
||||
if (self.unique && self.form?.parentStore?.storeType === 'ComboStore') {
|
||||
const combo = self.form.parentStore as IComboStore;
|
||||
const group = combo.uniques.get(self.name) as IUniqueGroup;
|
||||
|
||||
if (
|
||||
group.items.some(
|
||||
item =>
|
||||
item !== self &&
|
||||
self.tmpValue !== undefined &&
|
||||
item.value === self.tmpValue
|
||||
)
|
||||
) {
|
||||
addError(self.__('Form.unique'));
|
||||
}
|
||||
}
|
||||
|
||||
self.validating = false;
|
||||
return self.valid;
|
||||
});
|
||||
|
||||
function setError(msg: string | Array<string>, tag: string = 'builtin') {
|
||||
clearError();
|
||||
|
Loading…
Reference in New Issue
Block a user