1. Getting Started
  2. Validation

Getting Started

Validation

To validate a form 💯 you can use a core rules or customize your own.

Core rules

List
const fields = [{
  ...,
  rules: [
    'required',
    'min:number',
    'max:number',
    'between:number:number',
    'equal:number',
    'email',
    'url'
    'file'
  ]
}];

Custom rules

Example
import { Formly, type IField, getValues } from 'svelte-formly';

const form_name = 'custom_validation';
const fields: IField[] = [
	{
		type: 'input',
		name: 'firstname',
		attributes: {
			id: 'firstname'
		}
	},
	{
		type: 'input',
		name: 'lastname',
		attributes: {
			id: 'lastname'
		},
		rules: [
			'required',
			{ name: 'notEqual', fnc: notEqual },
			{ name: 'customRule2', fnc: customRule2 }
		],
		messages: {
			notEqual: 'Last name not equal to First name',
			customRule2: 'foo bar'
		}
	}
];

// Custom rule to force field
async function notEqual(): Promise<boolean> {
	const values: any = await getValues(form_name);
	if (values.firstname === values.lastname) {
		return false;
	}
	return true;
}

async function customRule2(): Promise<boolean> {
	// ...others conditions.
	return true;
}