FormyValidator<T> class abstract

A base class for creating custom field validators.

The FormyValidator can be used with FieldControllers holding values of type T. It provides an interface to implement validation logic by overriding onValidate.

If you implement a custom validator, override the onValidate method to define your specific validation rules.

Properties

  • message: An optional custom error message to display when invalid.

Example

class PositiveNumberValidator extends FormyValidator<int> {
  PositiveNumberValidator({super.message});

  @override
  ValidationResult onValidate(FieldController<int> controller) {
    final value = controller.value;
    return ValidationResult(
      key: 'positive',
      message: message,
      isValid: value != null ? value > 0 : true,
    );
  }
}

FieldController<int> field = FieldController(
  key: 'amount',
  validators: [PositiveNumberValidator()],
);

See also

Implementers

Constructors

FormyValidator.new({required String? message})

Properties

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

Methods

call(FieldController<T> controller) ValidationResult
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onValidate(FieldController<T> controller) ValidationResult
toString() String
A string representation of this object.
inherited

Operators

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