Custom User Input Validation
Overview
If you would like to perform custom user input validation for your component, add the following method
export function validateUserInput(): ValidationResult {}
Example
<script lang="ts">
import { ComponentLabel, convertDataAttributes, GroupSlot } from "@pragmatic-engineering/svelte-form-builder-[flavor]";
import type { ComponentOptions, Field, FormTab, ValidationResult } from "@pragmatic-engineering/svelte-form-builder-[flavor]/Utils/types";
export let componentOptions: ComponentOptions;
export let field: Field;
export let tab: FormTab;
export function validateUserInput(): ValidationResult {
if (field.htmlAttributes.value != "valid") {
return { field: field, errors: [`${field.labelAttributes?.label} is not valid...`] };
}
}
</script>
<GroupSlot>
<ComponentLabel field="{field}" />
<input {...field.htmlAttributes} {...convertDataAttributes(field.dataAttributes)} type="text" bind:value="{field.htmlAttributes.value}" />
</GroupSlot>
Demo
- Add the Custom Text component
- Go to Preview
- Open the Tools
- Click Validate (you should see errors)
- Enter ‘valid’ as the value and re-validate (you should see no errors)