Merge pull request #4033 from Budibase/fix/dynamic-validators

Fix dynamic validators not correctly validating form components
This commit is contained in:
Andrew Kingston 2022-01-18 19:20:27 +00:00 committed by GitHub
commit 603d38ad78
1 changed files with 17 additions and 10 deletions

View File

@ -141,8 +141,18 @@
return return
} }
// Create validation function based on field schema
const schemaConstraints = schema?.[field]?.constraints
const validator = createValidatorFromConstraints(
schemaConstraints,
validationRules,
field,
table
)
// If we've already registered this field then keep some existing state // If we've already registered this field then keep some existing state
let initialValue = deepGet(initialValues, field) ?? defaultValue let initialValue = deepGet(initialValues, field) ?? defaultValue
let initialError = null
let fieldId = `id-${generateID()}` let fieldId = `id-${generateID()}`
const existingField = getField(field) const existingField = getField(field)
if (existingField) { if (existingField) {
@ -156,20 +166,17 @@
} else { } else {
initialValue = fieldState.value ?? initialValue initialValue = fieldState.value ?? initialValue
} }
// If this field has already been registered and we previously had an
// error set, then re-run the validator to see if we can unset it
if (fieldState.error) {
initialError = validator(initialValue)
}
} }
// Auto columns are always disabled // Auto columns are always disabled
const isAutoColumn = !!schema?.[field]?.autocolumn const isAutoColumn = !!schema?.[field]?.autocolumn
// Create validation function based on field schema
const schemaConstraints = schema?.[field]?.constraints
const validator = createValidatorFromConstraints(
schemaConstraints,
validationRules,
field,
table
)
// Construct field info // Construct field info
const fieldInfo = writable({ const fieldInfo = writable({
name: field, name: field,
@ -178,7 +185,7 @@
fieldState: { fieldState: {
fieldId, fieldId,
value: initialValue, value: initialValue,
error: null, error: initialError,
disabled: disabled || fieldDisabled || isAutoColumn, disabled: disabled || fieldDisabled || isAutoColumn,
defaultValue, defaultValue,
validator, validator,