From 91e35ef816755b065f193d4208c97d43e1db7cf8 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Mon, 4 Apr 2022 08:49:01 +0100 Subject: [PATCH] Add internal setting to fully disable form validation --- .../src/components/app/forms/Form.svelte | 5 +++ .../src/components/app/forms/InnerForm.svelte | 33 +++++++++++-------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/packages/client/src/components/app/forms/Form.svelte b/packages/client/src/components/app/forms/Form.svelte index 8e2e0b2510..740c5fd9f1 100644 --- a/packages/client/src/components/app/forms/Form.svelte +++ b/packages/client/src/components/app/forms/Form.svelte @@ -9,6 +9,10 @@ export let disabled = false export let actionType = "Create" + // Not exposed as a builder setting. Used internally to disable validation + // for fields rendered in things like search blocks. + export let disableValidation = false + const context = getContext("context") const { API, fetchDatasourceSchema } = getContext("sdk") @@ -102,6 +106,7 @@ {schema} {table} {initialValues} + {disableValidation} > diff --git a/packages/client/src/components/app/forms/InnerForm.svelte b/packages/client/src/components/app/forms/InnerForm.svelte index b82070681b..ae6ae3ebcc 100644 --- a/packages/client/src/components/app/forms/InnerForm.svelte +++ b/packages/client/src/components/app/forms/InnerForm.svelte @@ -10,6 +10,7 @@ export let size export let schema export let table + export let disableValidation = false const component = getContext("component") const { styleable, Provider, ActionTypes } = getContext("sdk") @@ -141,12 +142,14 @@ // Create validation function based on field schema const schemaConstraints = schema?.[field]?.constraints - const validator = createValidatorFromConstraints( - schemaConstraints, - validationRules, - field, - table - ) + const validator = disableValidation + ? null + : createValidatorFromConstraints( + schemaConstraints, + validationRules, + field, + table + ) // If we've already registered this field then keep some existing state let initialValue = Helpers.deepGet(initialValues, field) ?? defaultValue @@ -164,7 +167,7 @@ // 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) + initialError = validator?.(initialValue) } } @@ -254,7 +257,7 @@ } // Update field state - const error = validator ? validator(value) : null + const error = validator?.(value) fieldInfo.update(state => { state.fieldState.value = value state.fieldState.error = error @@ -288,12 +291,14 @@ // Create new validator const schemaConstraints = schema?.[field]?.constraints - const validator = createValidatorFromConstraints( - schemaConstraints, - validationRules, - field, - table - ) + const validator = disableValidation + ? null + : createValidatorFromConstraints( + schemaConstraints, + validationRules, + field, + table + ) // Update validator fieldInfo.update(state => {