Adding validation to forms to ensure custom fields can't be the wrong type

This commit is contained in:
Andrew Kingston 2021-02-05 10:53:25 +00:00
parent f44f887648
commit 53491c400d
8 changed files with 51 additions and 23 deletions

View File

@ -1,5 +1,5 @@
<script> <script>
import SpectrumField from "./SpectrumField.svelte" import Field from "./Field.svelte"
import Dropzone from "../attachments/Dropzone.svelte" import Dropzone from "../attachments/Dropzone.svelte"
import { onMount } from "svelte" import { onMount } from "svelte"
@ -21,8 +21,14 @@
}) })
</script> </script>
<SpectrumField {label} {field} bind:fieldState bind:fieldApi defaultValue={[]}> <Field
{label}
{field}
type="attachment"
bind:fieldState
bind:fieldApi
defaultValue={[]}>
{#if mounted} {#if mounted}
<Dropzone bind:files={value} /> <Dropzone bind:files={value} />
{/if} {/if}
</SpectrumField> </Field>

View File

@ -1,7 +1,6 @@
<script> <script>
import { onMount } from "svelte"
import "@spectrum-css/checkbox/dist/index-vars.css" import "@spectrum-css/checkbox/dist/index-vars.css"
import SpectrumField from "./SpectrumField.svelte" import Field from "./Field.svelte"
export let field export let field
export let label export let label
@ -15,9 +14,10 @@
} }
</script> </script>
<SpectrumField <Field
{label} {label}
{field} {field}
type="boolean"
bind:fieldState bind:fieldState
bind:fieldApi bind:fieldApi
defaultValue={false}> defaultValue={false}>
@ -48,4 +48,4 @@
</label> </label>
</div> </div>
{/if} {/if}
</SpectrumField> </Field>

View File

@ -1,6 +1,6 @@
<script> <script>
import Flatpickr from "svelte-flatpickr" import Flatpickr from "svelte-flatpickr"
import SpectrumField from "./SpectrumField.svelte" import Field from "./Field.svelte"
import "flatpickr/dist/flatpickr.css" import "flatpickr/dist/flatpickr.css"
import "@spectrum-css/inputgroup/dist/index-vars.css" import "@spectrum-css/inputgroup/dist/index-vars.css"
@ -53,7 +53,7 @@
} }
</script> </script>
<SpectrumField {label} {field} bind:fieldState bind:fieldApi> <Field {label} {field} type="datetime" bind:fieldState bind:fieldApi>
{#if fieldState} {#if fieldState}
<Flatpickr <Flatpickr
bind:flatpickr bind:flatpickr
@ -114,7 +114,7 @@
<div class="overlay" on:mousedown|self={flatpickr?.close} /> <div class="overlay" on:mousedown|self={flatpickr?.close} />
{/if} {/if}
{/if} {/if}
</SpectrumField> </Field>
<style> <style>
.spectrum-Textfield-input { .spectrum-Textfield-input {

View File

@ -9,6 +9,7 @@
export let fieldApi export let fieldApi
export let fieldSchema export let fieldSchema
export let defaultValue export let defaultValue
export let type
// Get contexts // Get contexts
const formContext = getContext("form") const formContext = getContext("form")
@ -45,6 +46,10 @@
<Placeholder> <Placeholder>
Add the Field setting to start using your component Add the Field setting to start using your component
</Placeholder> </Placeholder>
{:else if fieldSchema?.type && fieldSchema?.type !== type}
<Placeholder>
This Field setting is the wrong data type for this component
</Placeholder>
{:else} {:else}
<slot /> <slot />
{#if $fieldState.error} {#if $fieldState.error}

View File

@ -1,7 +1,7 @@
<script> <script>
import { onMount } from "svelte" import { onMount } from "svelte"
import { RichText } from "@budibase/bbui" import { RichText } from "@budibase/bbui"
import SpectrumField from "./SpectrumField.svelte" import Field from "./Field.svelte"
export let field export let field
export let label export let label
@ -38,13 +38,19 @@
} }
</script> </script>
<SpectrumField {label} {field} bind:fieldState bind:fieldApi defaultValue=""> <Field
{label}
{field}
type="longform"
bind:fieldState
bind:fieldApi
defaultValue="">
{#if mounted} {#if mounted}
<div> <div>
<RichText bind:value {options} /> <RichText bind:value {options} />
</div> </div>
{/if} {/if}
</SpectrumField> </Field>
<style> <style>
div { div {

View File

@ -1,5 +1,5 @@
<script> <script>
import SpectrumField from "./SpectrumField.svelte" import Field from "./Field.svelte"
import Picker from "./Picker.svelte" import Picker from "./Picker.svelte"
export let field export let field
@ -23,7 +23,13 @@
} }
</script> </script>
<SpectrumField {field} {label} bind:fieldState bind:fieldApi bind:fieldSchema> <Field
{field}
{label}
type="options"
bind:fieldState
bind:fieldApi
bind:fieldSchema>
{#if fieldState} {#if fieldState}
<Picker <Picker
bind:open bind:open
@ -35,4 +41,4 @@
isOptionSelected={option => option === $fieldState.value} isOptionSelected={option => option === $fieldState.value}
onSelectOption={selectOption} /> onSelectOption={selectOption} />
{/if} {/if}
</SpectrumField> </Field>

View File

@ -1,6 +1,6 @@
<script> <script>
import { getContext } from "svelte" import { getContext } from "svelte"
import SpectrumField from "./SpectrumField.svelte" import Field from "./Field.svelte"
import Picker from "./Picker.svelte" import Picker from "./Picker.svelte"
const { API } = getContext("sdk") const { API } = getContext("sdk")
@ -62,9 +62,10 @@
} }
</script> </script>
<SpectrumField <Field
{label} {label}
{field} {field}
type="link"
bind:fieldState bind:fieldState
bind:fieldApi bind:fieldApi
bind:fieldSchema bind:fieldSchema
@ -77,4 +78,4 @@
onSelectOption={toggleOption} onSelectOption={toggleOption}
getOptionLabel={getDisplayName} getOptionLabel={getDisplayName}
getOptionValue={option => option._id} /> getOptionValue={option => option._id} />
</SpectrumField> </Field>

View File

@ -1,7 +1,6 @@
<script> <script>
import { onMount } from "svelte"
import "@spectrum-css/textfield/dist/index-vars.css" import "@spectrum-css/textfield/dist/index-vars.css"
import SpectrumField from "./SpectrumField.svelte" import Field from "./Field.svelte"
export let field export let field
export let label export let label
@ -31,7 +30,12 @@
} }
</script> </script>
<SpectrumField {label} {field} bind:fieldState bind:fieldApi> <Field
{label}
{field}
type={type === 'number' ? 'number' : 'string'}
bind:fieldState
bind:fieldApi>
{#if fieldState} {#if fieldState}
<div class="spectrum-Textfield" class:is-invalid={!$fieldState.valid}> <div class="spectrum-Textfield" class:is-invalid={!$fieldState.valid}>
{#if !$fieldState.valid} {#if !$fieldState.valid}
@ -53,4 +57,4 @@
class="spectrum-Textfield-input" /> class="spectrum-Textfield-input" />
</div> </div>
{/if} {/if}
</SpectrumField> </Field>