Skip to content

Commit aa8c84f

Browse files
Toavina23alestiago
andauthored
feat: add purety check to FormzMixin (#87)
* feat: add purety check to FormzMixin * fix: comment typos Co-authored-by: Alejandro Santiago <dev@alestiago.com> * fix comment typos Co-authored-by: Alejandro Santiago <dev@alestiago.com> * fix comment typos Co-authored-by: Alejandro Santiago <dev@alestiago.com> --------- Co-authored-by: Alejandro Santiago <dev@alestiago.com>
1 parent 1b8334f commit aa8c84f

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

lib/formz.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ class Formz {
151151
static bool validate(List<FormzInput<dynamic, dynamic>> inputs) {
152152
return inputs.every((input) => input.isValid);
153153
}
154+
155+
/// Returns a [bool] given a list of [FormzInput] indicating whether
156+
/// all the inputs are pure.
157+
static bool isPure(List<FormzInput<dynamic, dynamic>> inputs) {
158+
return inputs.every((input) => input.isPure);
159+
}
154160
}
155161

156162
/// Mixin that automatically handles validation of all [FormzInput]s present in
@@ -180,6 +186,12 @@ mixin FormzMixin {
180186
/// Whether the [FormzInput] values are not all valid.
181187
bool get isNotValid => !isValid;
182188

189+
/// Whether all of the [FormzInput] are pure.
190+
bool get isPure => Formz.isPure(inputs);
191+
192+
/// Whether all of the [FormzInput] are dirty.
193+
bool get isDirty => !isPure;
194+
183195
/// Returns all [FormzInput] instances.
184196
///
185197
/// Override this and give it all [FormzInput]s in your class that should be

test/formz_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ void main() {
2626
expect(form.isValid, isTrue);
2727
expect(form.isNotValid, isFalse);
2828
});
29+
30+
test('is pure when none of the inputs were touched', () {
31+
final form = NameInputFormzMixin();
32+
expect(form.isPure, isTrue);
33+
expect(form.isDirty, isFalse);
34+
});
35+
36+
test('is dirty when one or multiple inputs were touched', () {
37+
final form = NameInputFormzMixin(name: const NameInput.dirty());
38+
expect(form.isDirty, isTrue);
39+
expect(form.isPure, isFalse);
40+
});
2941
});
3042

3143
group('FormzInputErrorCacheMixin', () {

0 commit comments

Comments
 (0)