reset method
Resets the control, marking it as untouched, pristine and setting the value to null.
In case of FormGroup or FormArray all descendants are marked pristine and untouched, and the value of all descendants are set to null.
The argument value
is optional and resets the control with an initial
value.
The argument disabled
is optional and resets the disabled status of the
control. If value is true
then if will disable the control, if value is
false
then if will enable the control, and if the value is null
or
not set (the default) then the control will state in the same state that
it previously has.
The argument removeFocus
is optional and remove the UI focus from the
control. In case of FormGroup or FormArray remove the focus from all
descendants.
When updateParent
is true or not supplied (the default) each change
affects this control and its parent, otherwise only affects to this
control.
When emitEvent
is true or not supplied (the default), both the
statusChanges and valueChanges events notify listeners with the
latest status and value when the control is reset. When false, no events
are emitted.
FormControl example
final control = FormControl<String>();
control.reset(value: 'John Doe');
print(control.value); // output: 'John Doe'
FormGroup example
final form = FormGroup({
'first': FormControl(value: 'first name'),
'last': FormControl(value: 'last name'),
});
print(form.value); // output: {first: 'first name', last: 'last name'}
form.reset(value: { 'first': 'John', 'last': 'last name' });
print(form.value); // output: {first: 'John', last: 'last name'}
FormArray example
final array = FormArray<String>([
FormControl<String>(),
FormControl<String>(),
]);
array.reset(value: ['name', 'last name']);
print(array.value); // output: ['name', 'last name']
```
Implementation
void reset({
T? value,
bool updateParent = true,
bool emitEvent = true,
bool removeFocus = false,
bool? disabled,
}) {
markAsPristine(updateParent: updateParent);
markAsUntouched(updateParent: updateParent);
updateValue(value, updateParent: updateParent, emitEvent: emitEvent);
if (disabled != null) {
disabled
? markAsDisabled(updateParent: true, emitEvent: false)
: markAsEnabled(updateParent: true, emitEvent: false);
}
if (removeFocus) {
unfocus(touched: false);
}
}