Skip to content

Commit

Permalink
feat(subform): subform required true, and all fields of the subform a…
Browse files Browse the repository at this point in the history
…re required
  • Loading branch information
Liberty-liu committed Sep 6, 2023
1 parent 49f706f commit bcbfeb8
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions packages/hooks/use-props/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import _ from 'lodash-es'
import Region from '@ER/region/Region'
import { areaList } from '@vant/area-data'
import { useI18n } from '../use-i18n'
import utils from '@ER/utils'
const findPosition = (node, parent) => {
for (let y = 0; y < parent.list.length; y++) {
const row = parent.list[y]
Expand All @@ -16,6 +17,15 @@ const findPosition = (node, parent) => {

return { x: -1, y: -1 }
}
const findAllFields = (node) => {
const result = []
node.list.forEach(e => {
e.forEach(e => {
result.push(...e.columns)
})
})
return result
}
const addValidate = (result, node, isPc, t) => {
const {
options
Expand Down Expand Up @@ -54,31 +64,39 @@ const addValidate = (result, node, isPc, t) => {

const obj = {
}
// if (node.type === 'select') {
// // obj.type = 'array'
// }
const validator = (...arg0) => new Promise((...arg1) => {
const resolve = arg1[0]
const resolve = () => {
arg1[0]()
}
const reject = isPc
? arg1[1]
: (message) => {
obj.message = message
arg1[0](false)
}
// const value = options.isShowTrim ? (isPc ? arg0[1] : arg0[0]).trim() : (isPc ? arg0[1] : arg0[0])
// let message
// let result = true
// let msg = ''
let value = isPc ? arg0[1] : arg0[0]
// only for mobile
if (/^(signature|radio|checkbox|select|html)$/.test(node.type)) {
value = options.defaultValue
}
const newValue = options.isShowTrim ? value.trim() : value
// if (options.required && (!newValue || newValue === null || newValue === undefined || (Array.isArray(newValue) && !newValue.length))) {
if (result.required && (newValue === '' || newValue === null || newValue === undefined || (Array.isArray(newValue) && !newValue.length))) {
reject(t('er.validateMsg.required'))
return
if (node.type === 'subform') {
if (result.required && findAllFields(node).some(e => utils.isEmpty(e.options.isShowTrim ? e.options.defaultValue.trim() : e.options.defaultValue))) {
reject(t('er.validateMsg.required'))
} else {
resolve()
}
} else {
let isRequired = result.required
if (utils.checkIsInSubform(node)) {
if (node.context.parent.context.parent.options.required) {
isRequired = true
}
}
if (isRequired && node.type !== 'subform' && utils.isEmpty(newValue)) {
reject(t('er.validateMsg.required'))
return
}
}
switch (node.type) {
case 'input':
Expand Down

0 comments on commit bcbfeb8

Please sign in to comment.