Skip to content

Commit 3b53da4

Browse files
committed
Merge branch 'release/18.1.2'
2 parents ec7023a + 4744469 commit 3b53da4

File tree

5 files changed

+80
-68
lines changed

5 files changed

+80
-68
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 18.1.2
2+
3+
## Fixes
4+
- Fixed an issue where enabling or disabling a `FormGroup` and `FormArray` did not correctly propagate the enabled/disabled state to all of its child controls.
5+
16
# 18.1.1
27

38
## Fixes

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ dependencies:
8888
flutter:
8989
sdk: flutter
9090

91-
reactive_forms: ^18.1.1
91+
reactive_forms: ^18.1.2
9292
```
9393
9494
Then, run the command `flutter packages get` in the console.

lib/src/models/models.dart

Lines changed: 62 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,7 @@ abstract class AbstractControl<T> {
410410
return;
411411
}
412412
_status = ControlStatus.valid;
413-
updateValueAndValidity(updateParent: true, emitEvent: emitEvent);
414-
_updateAncestors(updateParent);
413+
updateValueAndValidity(updateParent: updateParent, emitEvent: emitEvent);
415414
}
416415

417416
/// Disables the control.
@@ -1162,6 +1161,53 @@ abstract class FormControlCollection<T> extends AbstractControl<T> {
11621161

11631162
return result;
11641163
}
1164+
1165+
/// Enables the control.
1166+
///
1167+
/// This means the control is included in validation checks and the aggregate
1168+
/// value of its parent. Its status recalculates based on its value and its
1169+
/// validators.
1170+
///
1171+
/// When [updateParent] is false, mark only this control.
1172+
/// When true or not supplied (the default), marks all direct ancestors.
1173+
///
1174+
/// When [emitEvent] is true or not supplied (the default), [valueChanges]
1175+
/// and [statusChanged] events are emitted if value or status change.
1176+
/// Otherwise the control update this values but none of this events are
1177+
/// emitted.
1178+
@override
1179+
void markAsEnabled({bool updateParent = true, bool emitEvent = true}) {
1180+
forEachChild((control) {
1181+
control.markAsEnabled(updateParent: false, emitEvent: emitEvent);
1182+
});
1183+
1184+
updateValueAndValidity(updateParent: updateParent, emitEvent: emitEvent);
1185+
}
1186+
1187+
/// Disables the control.
1188+
///
1189+
/// This means the control is exempt from validation checks and excluded
1190+
/// from the aggregate value of any parent. Its status is `DISABLED`.
1191+
///
1192+
/// If the control has children, all children are also disabled.
1193+
///
1194+
/// When [updateParent] is false, mark only this control.
1195+
/// When true or not supplied (the default), marks all direct ancestors.
1196+
///
1197+
/// When [emitEvent] is true or not supplied (the default), [valueChanges]
1198+
/// and [statusChanged] events are emitted if value or status change.
1199+
/// Otherwise the control update this values but none of this events are
1200+
/// emitted.
1201+
@override
1202+
void markAsDisabled({bool updateParent = true, bool emitEvent = true}) {
1203+
forEachChild((control) {
1204+
control.markAsDisabled(updateParent: false, emitEvent: emitEvent);
1205+
});
1206+
1207+
_errors.clear();
1208+
1209+
updateValueAndValidity(updateParent: updateParent, emitEvent: emitEvent);
1210+
}
11651211
}
11661212

11671213
/// Tracks the value and validity state of a group of FormControl instances.
@@ -1350,49 +1396,6 @@ class FormGroup extends FormControlCollection<Map<String, Object?>> {
13501396
updateValue(value);
13511397
}
13521398

1353-
/// Disables the control.
1354-
///
1355-
/// This means the control is exempt from validation checks and excluded
1356-
/// from the aggregate value of any parent. Its status is `DISABLED`.
1357-
///
1358-
/// If the control has children, all children are also disabled.
1359-
///
1360-
/// When [updateParent] is false, mark only this control.
1361-
/// When true or not supplied (the default), marks all direct ancestors.
1362-
///
1363-
/// When [emitEvent] is true or not supplied (the default), [valueChanges]
1364-
/// and [statusChanged] events are emitted if value or status change.
1365-
/// Otherwise the control update this values but none of this events are
1366-
/// emitted.
1367-
@override
1368-
void markAsDisabled({bool updateParent = true, bool emitEvent = true}) {
1369-
_controls.forEach((_, control) {
1370-
control.markAsDisabled(updateParent: true, emitEvent: emitEvent);
1371-
});
1372-
super.markAsDisabled(updateParent: updateParent, emitEvent: emitEvent);
1373-
}
1374-
1375-
/// Enables the control.
1376-
///
1377-
/// This means the control is included in validation checks and the aggregate
1378-
/// value of its parent. Its status recalculates based on its value and its
1379-
/// validators.
1380-
///
1381-
/// When [updateParent] is false, mark only this control.
1382-
/// When true or not supplied (the default), marks all direct ancestors.
1383-
///
1384-
/// When [emitEvent] is true or not supplied (the default), [valueChanges]
1385-
/// and [statusChanged] events are emitted if value or status change.
1386-
/// Otherwise the control update this values but none of this events are
1387-
/// emitted.
1388-
@override
1389-
void markAsEnabled({bool updateParent = true, bool emitEvent = true}) {
1390-
_controls.forEach((_, control) {
1391-
control.markAsEnabled(updateParent: true, emitEvent: emitEvent);
1392-
});
1393-
super.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent);
1394-
}
1395-
13961399
/// Appends all [controls] to the group.
13971400
void addAll(Map<String, AbstractControl<dynamic>> controls) {
13981401
_controls.addAll(controls);
@@ -1787,29 +1790,23 @@ class FormArray<T> extends FormControlCollection<List<T?>> {
17871790
/// and value when the control is reset. When false, no events are emitted.
17881791
@override
17891792
void markAsDisabled({bool updateParent = true, bool emitEvent = true}) {
1793+
if (disabled) {
1794+
return;
1795+
}
1796+
17901797
for (final control in _controls) {
1791-
control.markAsDisabled(updateParent: true, emitEvent: emitEvent);
1798+
control.markAsDisabled(updateParent: false, emitEvent: emitEvent);
17921799
}
1793-
super.markAsDisabled(updateParent: updateParent, emitEvent: emitEvent);
1794-
}
17951800

1796-
/// Enables the control. This means the control is included in validation
1797-
/// checks and the aggregate value of its parent. Its status recalculates
1798-
/// based on its value and its validators.
1799-
///
1800-
/// When [updateParent] is true or not supplied (the default) each change
1801-
/// affects this control and its parent, otherwise only affects to this
1802-
/// control.
1803-
///
1804-
/// When [emitEvent] is true or not supplied (the default), both the
1805-
/// *statusChanges* and *valueChanges* emit events with the latest status
1806-
/// and value when the control is reset. When false, no events are emitted.
1807-
@override
1808-
void markAsEnabled({bool updateParent = true, bool emitEvent = true}) {
1809-
forEachChild((control) {
1810-
control.markAsEnabled(updateParent: true, emitEvent: emitEvent);
1811-
});
1812-
super.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent);
1801+
_status = ControlStatus.disabled;
1802+
_updateValue();
1803+
1804+
if (emitEvent) {
1805+
_valueChanges.add(value);
1806+
_statusChanges.add(_status);
1807+
}
1808+
1809+
_updateAncestors(updateParent);
18131810
}
18141811

18151812
/// Insert a [control] at the given [index] position.

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: reactive_forms
22
description: This is a model-driven approach to handling form inputs and validations, heavily inspired in Angular Reactive Forms.
3-
version: 18.1.1
3+
version: 18.1.2
44
homepage: "https://github.com/joanpablo/reactive_forms"
55

66
environment:

test/src/models/form_group_test.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,20 @@ void main() {
288288
});
289289

290290
// When: enable form
291+
print("form.enabled: ${form.enabled}");
292+
291293
form.markAsEnabled();
292294

295+
form.controls.forEach((name, control) {
296+
print("control[$name].enabled: ${control.enabled}");
297+
});
298+
293299
// Then: all controls are enabled
294-
expect(form.controls.values.every((control) => control.enabled), true);
300+
expect(
301+
form.controls.values.every((control) => control.enabled),
302+
true,
303+
reason: "Not all controls are enabled",
304+
);
295305
});
296306

297307
test('Group valid when invalid control is disable', () {

0 commit comments

Comments
 (0)