State-Based Validation
Overview
Basic Usage
import { resolve } from '@aurelia/kernel';
import { IValidationRules } from '@aurelia/validation';
export class RegistrationForm {
private validationRules = resolve(IValidationRules);
public username: string = '';
public constructor() {
this.validationRules
.on(this)
.ensure('username')
.satisfiesState(
'valid',
(value: string) => {
if (!value) return 'empty';
if (value.length < 3) return 'tooShort';
if (!/^[a-zA-Z0-9_]+$/.test(value)) return 'invalidChars';
return 'valid';
},
{
empty: 'Username is required.',
tooShort: 'Username must be at least 3 characters.',
invalidChars: 'Username can only contain letters, numbers, and underscores.'
}
);
}
}How It Works
Async State Validation
Access to Object Context
Complete Example
Important Notes
When to Use State-Based Validation
Last updated
Was this helpful?