WtValidator class

Ready-made form-field validators that plug straight into a TextFormField.validator.

Each factory returns a String? Function(String?) — null means valid, a string is the error message. Combine several with compose:

TextFormField(
  validator: WtValidator.compose([
    WtValidator.required(),
    WtValidator.email(),
  ]),
);

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

compose(List<FormFieldValidator<String>> validators) FormFieldValidator<String>
Runs validators in order and returns the first error (or null).
email([String message = 'Invalid email']) FormFieldValidator<String>
Fails when a non-empty value isn't a valid email. Empty passes — combine with required to also require a value.
match(String? other(), [String message = 'Values do not match']) FormFieldValidator<String>
Fails when the value doesn't equal the value returned by other (e.g. a password-confirmation field).
maxLength(int length, [String? message]) FormFieldValidator<String>
Fails when the value is longer than length characters.
minLength(int length, [String? message]) FormFieldValidator<String>
Fails when the value is shorter than length characters.
pattern(RegExp regex, [String message = 'Invalid format']) FormFieldValidator<String>
Fails when a non-empty value doesn't match regex.
phone([String message = 'Invalid phone number']) FormFieldValidator<String>
Fails when a non-empty value isn't a plausible phone number (digits, spaces and + - ( ), 7–15 digits).
required([String message = 'Required']) FormFieldValidator<String>
Fails when the value is null/blank (after trimming).