Merge pull request #7289 from 2betop/fix-input-group-validate

fix: 修复 inputGroup 表单项在 service 下时校验问题 Close: #7279
This commit is contained in:
hsm-lv 2023-06-29 14:46:31 +08:00 committed by GitHub
commit 8473def58b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 37 deletions

View File

@ -56,7 +56,11 @@ function mockResponse(event, context, callback) {
function createHeaders(headers) {
let referer = '';
if (/^(https?\:\/\/[^:\/]+(?:\:\d+)?\/)/i.test(headers['Referer'])) {
if (
/^(https?\:\/\/[^:\/]+(?:\:\d+)?\/)/i.test(
headers['Referer'] || headers['referer']
)
) {
referer = RegExp.$1.replace(/\/$/, '');
}

View File

@ -423,6 +423,7 @@ export function wrapControl<
? 'formInited'
: 'dataChanged'
);
this.checkValidate();
}
}
}
@ -513,32 +514,44 @@ export function wrapControl<
}
}
checkValidate() {
if (!this.model) return; // 如果 model 为 undefined 则直接返回
const validated = this.model.validated;
const {formSubmited, validateOnChange} = this.props;
if (
// 如果配置了 minLength 或者 maxLength 就切成及时验证
// this.model.rules.minLength ||
// this.model.rules.maxLength ||
validateOnChange === true ||
(validateOnChange !== false && (formSubmited || validated))
) {
this.validate();
} else if (validateOnChange === false) {
this.model?.reset();
}
}
async validate() {
if (!this.model) return;
const {formStore: form, data, formItemDispatchEvent} = this.props;
let result;
if (this.model) {
if (
this.model.unique &&
form?.parentStore &&
form.parentStore.storeType === ComboStore.name
) {
const combo = form.parentStore as IComboStore;
const group = combo.uniques.get(
this.model.name
) as IUniqueGroup;
const validPromises = group.items.map(item =>
item.validate(data)
);
result = await Promise.all(validPromises);
} else {
const validPromises = form
?.getItemsByName(this.model.name)
.map(item => item.validate(data));
if (validPromises && validPromises.length) {
result = await Promise.all(validPromises);
}
}
if (
this.model.unique &&
form?.parentStore &&
form.parentStore.storeType === ComboStore.name
) {
const combo = form.parentStore as IComboStore;
const group = combo.uniques.get(this.model.name) as IUniqueGroup;
const validPromises = group.items.map(item =>
item.validate(data)
);
result = await Promise.all(validPromises);
} else {
result = [await this.model.validate(data)];
}
if (result && result.length) {
if (result.indexOf(false) > -1) {
formItemDispatchEvent('formItemValidateError', data);
@ -655,20 +668,8 @@ export function wrapControl<
return;
}
const validated = this.model.validated;
onChange?.(value, name!, submitOnChange === true);
if (
// 如果配置了 minLength 或者 maxLength 就切成及时验证
// this.model.rules.minLength ||
// this.model.rules.maxLength ||
validateOnChange === true ||
(validateOnChange !== false && (formSubmited || validated))
) {
this.validate();
} else if (validateOnChange === false) {
this.model?.reset();
}
this.checkValidate();
}
handleBlur(e: any) {

View File

@ -92,7 +92,7 @@ export const FormStore = ServiceStore.named('FormStore')
/** 获取InputGroup的子元素 */
get inputGroupItems() {
const formItems: Record<string, IFormItemStore[]> = {};
const children = self.children.concat();
const children: Array<any> = this.directItems.concat();
while (children.length) {
const current = children.shift();

View File

@ -120,6 +120,9 @@ export class InputGroup extends React.Component<
}
const chidren = formStore?.inputGroupItems?.[name];
if (!Array.isArray(chidren)) {
return;
}
const errorCollection = chidren
.map((item, index) => {
if (item.errors.length <= 0) {