()
| 555 | } |
| 556 | |
| 557 | async validate(): Promise<string | void> { |
| 558 | const {value, translate: __, columns} = this.props; |
| 559 | const minLength = this.resolveVariableProps(this.props, 'minLength'); |
| 560 | const maxLength = this.resolveVariableProps(this.props, 'maxLength'); |
| 561 | |
| 562 | // todo: 如果当前正在编辑中,表单提交了,应该先让正在编辑的东西提交然后再做验证。 |
| 563 | if (this.state.editIndex) { |
| 564 | return __('Table.editing'); |
| 565 | } |
| 566 | |
| 567 | if (minLength && (!Array.isArray(value) || value.length < minLength)) { |
| 568 | return __('Combo.minLength', {minLength}); |
| 569 | } else if (maxLength && Array.isArray(value) && value.length > maxLength) { |
| 570 | return __('Combo.maxLength', {maxLength}); |
| 571 | } else { |
| 572 | const subForms: Array<any> = []; |
| 573 | Object.keys(this.subForms).forEach( |
| 574 | key => this.subForms[key] && subForms.push(this.subForms[key]) |
| 575 | ); |
| 576 | if (subForms.length) { |
| 577 | const results = await Promise.all( |
| 578 | subForms.map(item => item.validate()) |
| 579 | ); |
| 580 | |
| 581 | let msg = ~results.indexOf(false) ? __('Form.validateFailed') : ''; |
| 582 | let uniqueColumn = ''; |
| 583 | |
| 584 | if ( |
| 585 | !msg && |
| 586 | Array.isArray(columns) && |
| 587 | Array.isArray(value) && |
| 588 | columns.some(item => { |
| 589 | if (item.unique && item.name) { |
| 590 | let exists: Array<any> = []; |
| 591 | |
| 592 | return value.some((obj: any) => { |
| 593 | const value = getVariable(obj, item.name); |
| 594 | |
| 595 | if (~exists.indexOf(value)) { |
| 596 | uniqueColumn = `${item.label || item.name}`; |
| 597 | return true; |
| 598 | } |
| 599 | |
| 600 | exists.push(value); |
| 601 | return false; |
| 602 | }); |
| 603 | } |
| 604 | |
| 605 | return false; |
| 606 | }) |
| 607 | ) { |
| 608 | msg = __('InputTable.uniqueError', { |
| 609 | label: uniqueColumn |
| 610 | }); |
| 611 | } |
| 612 | |
| 613 | if (msg) { |
| 614 | return msg; |
no test coverage detected