Form: setting default values on record fields

This commit is contained in:
Michael Shanks 2020-07-21 12:16:05 +01:00
parent 8cf8131057
commit 99c42786bb
2 changed files with 52 additions and 8 deletions

View File

@ -32,11 +32,35 @@
$: Object.values(inputElements).length && setForm(record) $: Object.values(inputElements).length && setForm(record)
const createBlankRecord = () => {
if (!schema) return
const newrecord = {
modelId: model,
}
for (let fieldName in schema) {
const field = schema[fieldName]
// defaulting to first one, as a blank value will fail validation
if (
field.type === "string" &&
field.constraints &&
field.constraints.inclusion &&
field.constraints.inclusion.length > 0
) {
newrecord[fieldName] = field.constraints.inclusion[0]
} else if (field.type === "number") newrecord[fieldName] = null
else if (field.type === "boolean") newrecord[fieldName] = false
else if (field.type === "link") newrecord[fieldName] = []
else newrecord[fieldName] = ""
}
return newrecord
}
async function fetchModel() { async function fetchModel() {
const FETCH_MODEL_URL = `/api/models/${model}` const FETCH_MODEL_URL = `/api/models/${model}`
const response = await _bb.api.get(FETCH_MODEL_URL) const response = await _bb.api.get(FETCH_MODEL_URL)
modelDef = await response.json() modelDef = await response.json()
schema = modelDef.schema schema = modelDef.schema
record = createBlankRecord()
} }
async function save() { async function save() {
@ -81,9 +105,7 @@
el.checked = false el.checked = false
} }
} }
record = { record = createBlankRecord()
modelId: model,
}
} }
const setForm = rec => { const setForm = rec => {
@ -123,7 +145,7 @@
isNew = !recordId || recordId === "new" isNew = !recordId || recordId === "new"
if (isNew) { if (isNew) {
record = { modelId: model } record = createBlankRecord()
} else { } else {
const GET_RECORD_URL = `/api/${model}/records/${recordId}` const GET_RECORD_URL = `/api/${model}/records/${recordId}`
_bb.api _bb.api

View File

@ -32,11 +32,35 @@
$: Object.values(inputElements).length && setForm(record) $: Object.values(inputElements).length && setForm(record)
const createBlankRecord = () => {
if (!schema) return
const newrecord = {
modelId: model,
}
for (let fieldName in schema) {
const field = schema[fieldName]
// defaulting to first one, as a blank value will fail validation
if (
field.type === "string" &&
field.constraints &&
field.constraints.inclusion &&
field.constraints.inclusion.length > 0
) {
newrecord[fieldName] = field.constraints.inclusion[0]
} else if (field.type === "number") newrecord[fieldName] = null
else if (field.type === "boolean") newrecord[fieldName] = false
else if (field.type === "link") newrecord[fieldName] = []
else newrecord[fieldName] = ""
}
return newrecord
}
async function fetchModel() { async function fetchModel() {
const FETCH_MODEL_URL = `/api/models/${model}` const FETCH_MODEL_URL = `/api/models/${model}`
const response = await _bb.api.get(FETCH_MODEL_URL) const response = await _bb.api.get(FETCH_MODEL_URL)
modelDef = await response.json() modelDef = await response.json()
schema = modelDef.schema schema = modelDef.schema
record = createBlankRecord()
} }
async function save() { async function save() {
@ -81,9 +105,7 @@
el.checked = false el.checked = false
} }
} }
record = { record = createBlankRecord()
modelId: model,
}
} }
const setForm = rec => { const setForm = rec => {
@ -123,7 +145,7 @@
isNew = !recordId || recordId === "new" isNew = !recordId || recordId === "new"
if (isNew) { if (isNew) {
record = { modelId: model } record = createBlankRecord()
} else { } else {
const GET_RECORD_URL = `/api/${model}/records/${recordId}` const GET_RECORD_URL = `/api/${model}/records/${recordId}`
_bb.api _bb.api