Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/src/form_builder_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class FormBuilderFieldState<F extends FormBuilderField<T>, T>

void _informFormForFieldChange() {
if (_formBuilderState != null) {
_dirty = true;
_dirty = value != initialValue;
if (enabled || readOnly) {
_formBuilderState!.setInternalFieldValue<T>(widget.name, value);
return;
Comment thread
majumdersubhanu marked this conversation as resolved.
Expand Down Expand Up @@ -224,8 +224,8 @@ class FormBuilderFieldState<F extends FormBuilderField<T>, T>
/// Also reset custom error text if exists, and set [isDirty] to `false`.
void reset() {
super.reset();
didChange(initialValue);
_dirty = false;
didChange(initialValue);
if (_customErrorText != null) {
setState(() => _customErrorText = null);
}
Expand Down
52 changes: 52 additions & 0 deletions test/src/form_builder_field_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,27 @@ void main() {
expect(textFieldKey.currentState?.isDirty, true);
},
);
testWidgets('Should not dirty when value returns to initial by user', (
tester,
) async {
const textFieldName = 'text';
final textFieldKey = GlobalKey<FormBuilderFieldState>();
final testWidget = FormBuilderTextField(
name: textFieldName,
key: textFieldKey,
initialValue: 'hi',
);
await tester.pumpWidget(buildTestableFieldWidget(testWidget));

final widgetFinder = find.byWidget(testWidget);
await tester.enterText(widgetFinder, 'test');
await tester.pumpAndSettle();
expect(textFieldKey.currentState?.isDirty, true);

await tester.enterText(widgetFinder, 'hi');
await tester.pumpAndSettle();
expect(textFieldKey.currentState?.isDirty, false);
});
testWidgets('Should not dirty when reset field value', (tester) async {
const textFieldName = 'text';
final textFieldKey = GlobalKey<FormBuilderFieldState>();
Expand Down Expand Up @@ -481,6 +502,37 @@ void main() {
});
});
group('reset -', () {
testWidgets(
'Should avoid reset recursion when value returns to initial in onChanged',
(tester) async {
const textFieldName = 'text';
final textFieldKey = GlobalKey<FormBuilderFieldState>();
var onChangedCalls = 0;
final testWidget = FormBuilderTextField(
name: textFieldName,
key: textFieldKey,
initialValue: 'hi',
onChanged: (value) {
onChangedCalls++;
final state = textFieldKey.currentState;
if (value == state?.initialValue && state?.isDirty == true) {
state?.reset();
}
},
);
await tester.pumpWidget(buildTestableFieldWidget(testWidget));

final widgetFinder = find.byWidget(testWidget);
await tester.enterText(widgetFinder, 'test');
await tester.pumpAndSettle();
await tester.enterText(widgetFinder, 'hi');
await tester.pumpAndSettle();

expect(tester.takeException(), isNull);
expect(textFieldKey.currentState?.isDirty, false);
expect(onChangedCalls, equals(2));
},
Comment thread
majumdersubhanu marked this conversation as resolved.
);
testWidgets('Should reset to null when call reset', (tester) async {
const textFieldName = 'text';
final textFieldKey = GlobalKey<FormBuilderFieldState>();
Expand Down
21 changes: 21 additions & 0 deletions test/src/form_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,27 @@ void main() {

expect(formKey.currentState?.isDirty, true);
});
testWidgets('Should not dirty when value returns to initial by user', (
tester,
) async {
const textFieldName = 'text';
final testWidget = FormBuilderTextField(name: textFieldName);
await tester.pumpWidget(
buildTestableFieldWidget(
testWidget,
initialValue: {textFieldName: 'hi'},
),
);

final widgetFinder = find.byWidget(testWidget);
await tester.enterText(widgetFinder, 'test');
await tester.pumpAndSettle();
expect(formKey.currentState?.isDirty, true);

await tester.enterText(widgetFinder, 'hi');
await tester.pumpAndSettle();
expect(formKey.currentState?.isDirty, false);
});
testWidgets('Should dirty when update field with initial value by method', (
tester,
) async {
Expand Down
Loading