Pushed default parsing up into the InnerForm component to ensure that invalid default values are removed entirely. Fixes for custom validation for array types

This commit is contained in:
Dean 2022-10-18 16:24:36 +01:00
parent b31f7f4b87
commit 0e4a7ace08
5 changed files with 31 additions and 28 deletions

View File

@ -15,25 +15,12 @@
export let sort = false
export let autoWidth = false
$: streamed = Array.isArray(value)
? value.reduce((acc, entry) => {
if (typeof ele === "string" && entry.trim() === "") {
return acc
}
let processedOption = String(entry)
if (options.indexOf(processedOption) > -1) {
acc.push(processedOption)
}
return acc
}, [])
: []
const dispatch = createEventDispatcher()
$: selectedLookupMap = getSelectedLookupMap(streamed)
$: selectedLookupMap = getSelectedLookupMap(value)
$: optionLookupMap = getOptionLookupMap(options)
$: fieldText = getFieldText(streamed, optionLookupMap, placeholder)
$: fieldText = getFieldText(value, optionLookupMap, placeholder)
$: isOptionSelected = optionValue => selectedLookupMap[optionValue] === true
$: toggleOption = makeToggleOption(selectedLookupMap, streamed)
$: toggleOption = makeToggleOption(selectedLookupMap, value)
const getFieldText = (value, map, placeholder) => {
if (Array.isArray(value) && value.length > 0) {

View File

@ -36,7 +36,7 @@
const getFieldText = (value, options, placeholder) => {
// Always use placeholder if no value
if (value == null || value === "") {
if (value == null || value === "" || value.length == 0) {
return placeholder || "Choose an option"
}

View File

@ -305,6 +305,9 @@
getOptionLabel={x => x}
getOptionValue={x => x}
value={rule.value}
on:change={e => {
rule.value = e.detail
}}
/>
{:else if rule.type === "boolean"}
<Select

View File

@ -27,19 +27,12 @@
$: formField = formApi?.registerField(
field,
type,
parseDefaultValue(defaultValue),
defaultValue,
disabled,
validation,
formStep
)
const parseDefaultValue = defaultValue => {
if (Array.isArray(defaultValue) && type === "array") {
return defaultValue.map(val => String(val))
}
return defaultValue
}
$: schemaType = fieldSchema?.type !== "formula" ? fieldSchema?.type : "string"
// Focus label when editing

View File

@ -128,6 +128,23 @@
return fields.find(field => get(field).name === name)
}
const getDefault = (defaultValue, schema, type) => {
// Remove any values not present in the field schema
// Convert any values supplied to string
if (Array.isArray(defaultValue) && type == "array") {
return defaultValue.reduce((acc, entry) => {
let processedOption = String(entry)
let schemaOptions = schema.constraints.inclusion
if (schemaOptions.indexOf(processedOption) > -1) {
acc.push(processedOption)
}
return acc
}, [])
} else {
return defaultValue
}
}
const formApi = {
registerField: (
field,
@ -143,6 +160,7 @@
// Create validation function based on field schema
const schemaConstraints = schema?.[field]?.constraints
const validator = disableValidation
? null
: createValidatorFromConstraints(
@ -152,8 +170,10 @@
table
)
const parsedDefault = getDefault(defaultValue, schema?.[field], type)
// If we've already registered this field then keep some existing state
let initialValue = Helpers.deepGet(initialValues, field) ?? defaultValue
let initialValue = Helpers.deepGet(initialValues, field) ?? parsedDefault
let initialError = null
let fieldId = `id-${Helpers.uuid()}`
const existingField = getField(field)
@ -186,11 +206,11 @@
error: initialError,
disabled:
disabled || fieldDisabled || (isAutoColumn && !editAutoColumns),
defaultValue,
defaultValue: parsedDefault,
validator,
lastUpdate: Date.now(),
},
fieldApi: makeFieldApi(field, defaultValue),
fieldApi: makeFieldApi(field, parsedDefault),
fieldSchema: schema?.[field] ?? {},
})