Skip to content

Commit 4f24f87

Browse files
committed
Add some extensions
1 parent 72ce35f commit 4f24f87

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

lib/src/form_field_validator_extensions.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,42 @@ extension FormFieldValidatorExtensions<T> on FormFieldValidator<T> {
1212
return FormBuilderValidators.or([this, other]);
1313
}
1414

15+
/// Combines the current validator with another validator using logical XOR.
16+
FormFieldValidator<T> not() {
17+
return FormBuilderValidators.notEqual(this);
18+
}
19+
1520
/// Adds a condition to apply the validator only if the condition is met.
1621
FormFieldValidator<T> when(bool Function(T value) condition) {
1722
return FormBuilderValidators.conditional(condition, this);
1823
}
24+
25+
/// Adds a condition to apply the validator only if the condition is not met.
26+
FormFieldValidator<T> unless(bool Function(T value) condition) {
27+
return FormBuilderValidators.conditional(
28+
(value) => !condition(value), this);
29+
}
30+
31+
/// Transforms the value before applying the validator.
32+
FormFieldValidator<T> transform(T Function(T? value) transformer) {
33+
return FormBuilderValidators.transform(this, transformer);
34+
}
35+
36+
/// Skips the validator if the condition is met.
37+
FormFieldValidator<T> skipWhen(bool Function(T? value) condition) {
38+
return FormBuilderValidators.skipWhen(condition, this);
39+
}
40+
41+
/// Logs the value during the validation process.
42+
FormFieldValidator<T> log({String Function(T? value)? log}) {
43+
return FormBuilderValidators.log(log: log);
44+
}
45+
46+
/// Overrides the error message of the current validator.
47+
FormFieldValidator<T> withMessage(String errorMessage) {
48+
return (valueCandidate) {
49+
final result = this(valueCandidate);
50+
return result != null ? errorMessage : null;
51+
};
52+
}
1953
}

test/form_builder_validators_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'dart:io';
2-
import 'dart:math';
32

43
import 'package:flutter/material.dart';
54
import 'package:flutter_localizations/flutter_localizations.dart';

0 commit comments

Comments
 (0)