Skip to content

Commit 2d95e82

Browse files
committed
chore: code cleanups
1 parent bb1e861 commit 2d95e82

File tree

3 files changed

+37
-23
lines changed

3 files changed

+37
-23
lines changed

example/lib/sources/signup_form.dart

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class SignupForm extends StatefulWidget {
99

1010
class _SignupFormState extends State<SignupForm> {
1111
final _formKey = GlobalKey<FormBuilderState>();
12+
final _emailFieldKey = GlobalKey<FormBuilderFieldState>();
1213

1314
@override
1415
Widget build(BuildContext context) {
@@ -18,7 +19,7 @@ class _SignupFormState extends State<SignupForm> {
1819
padding: const EdgeInsets.all(8.0),
1920
child: FormBuilder(
2021
key: _formKey,
21-
autovalidateMode: AutovalidateMode.onUserInteraction,
22+
// autovalidateMode: AutovalidateMode.onUserInteraction,
2223
child: Column(
2324
children: [
2425
FormBuilderTextField(
@@ -30,6 +31,7 @@ class _SignupFormState extends State<SignupForm> {
3031
),
3132
const SizedBox(height: 10),
3233
FormBuilderTextField(
34+
key: _emailFieldKey,
3335
name: 'email',
3436
decoration: InputDecoration(labelText: 'Email'),
3537
validator: FormBuilderValidators.compose([
@@ -53,10 +55,9 @@ class _SignupFormState extends State<SignupForm> {
5355
autovalidateMode: AutovalidateMode.onUserInteraction,
5456
decoration: InputDecoration(
5557
labelText: 'Confirm Password',
56-
suffixIcon: (_formKey.currentState != null &&
57-
!(_formKey.currentState?.fields['confirm_password']
58-
?.isValid ??
59-
false))
58+
suffixIcon: ((_formKey.currentState
59+
?.fields['confirm_password']?.hasError ??
60+
false))
6061
? const Icon(Icons.error, color: Colors.red)
6162
: const Icon(Icons.check, color: Colors.green),
6263
),
@@ -106,6 +107,14 @@ class _SignupFormState extends State<SignupForm> {
106107
color: Theme.of(context).colorScheme.secondary,
107108
onPressed: () {
108109
if (_formKey.currentState?.saveAndValidate() ?? false) {
110+
if (true) {
111+
// Either invalidate using Form Key
112+
_formKey.currentState?.invalidateField(
113+
name: 'email', errorText: 'Email already taken.');
114+
// OR invalidate using Field Key
115+
// _emailFieldKey.currentState?.invalidate('Email already taken.');
116+
}
117+
109118
print('Valid');
110119
} else {
111120
print('Invalid');

lib/src/form_builder.dart

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,11 @@ class FormBuilderState extends State<FormBuilder> {
9595
Map<String, FormBuilderFieldState> get fields => _fields;
9696

9797
void setInternalFieldValue(String name, dynamic value) {
98-
setState(() {
99-
_value[name] = value;
100-
});
98+
setState(() => _value[name] = value);
10199
}
102100

103101
void removeInternalFieldValue(String name) {
104-
setState(() {
105-
_value.remove(name);
106-
});
102+
setState(() => _value.remove(name));
107103
}
108104

109105
void registerField(String name, FormBuilderFieldState field) {
@@ -155,13 +151,13 @@ class FormBuilderState extends State<FormBuilder> {
155151
fields.values.first.invalidate(errorText);
156152

157153
bool validate() {
158-
final validation = _formKey.currentState!.validate();
159-
if (!validation) {
154+
final hasError = !_formKey.currentState!.validate();
155+
if (hasError) {
160156
final wrongFields =
161157
fields.values.where((element) => element.hasError).toList();
162158
wrongFields.first.requestFocus();
163159
}
164-
return validation;
160+
return !hasError;
165161
}
166162

167163
bool saveAndValidate() {

lib/src/form_builder_field.dart

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,15 @@ class FormBuilderFieldState<F extends FormBuilderField<T?>, T>
101101
FormBuilderState? _formBuilderState;
102102

103103
@override
104-
bool get hasError => super.hasError || widget.decoration.errorText != null;
104+
String? get errorText => super.errorText ?? _customErrorText;
105105

106106
@override
107-
bool get isValid => super.isValid && widget.decoration.errorText == null;
107+
bool get hasError =>
108+
super.hasError || decoration.errorText != null || errorText != null;
109+
110+
@override
111+
bool get isValid =>
112+
super.isValid && decoration.errorText == null && errorText == null;
108113

109114
bool _touched = false;
110115

@@ -175,22 +180,26 @@ class FormBuilderFieldState<F extends FormBuilderField<T?>, T>
175180
}
176181

177182
@override
178-
bool validate() {
179-
setState(() => _customErrorText = null);
180-
return super.validate() && widget.decoration.errorText == null;
183+
bool validate({bool clearCustomError = true}) {
184+
print(clearCustomError);
185+
if (clearCustomError) {
186+
setState(() => _customErrorText = null);
187+
}
188+
return super.validate() && !hasError;
181189
}
182190

183191
void requestFocus() {
184192
FocusScope.of(context).requestFocus(effectiveFocusNode);
185193
Scrollable.ensureVisible(context);
186194
}
187195

188-
void invalidate(String reason) {
189-
setState(() => _customErrorText = reason);
196+
void invalidate(String errorText) {
197+
setState(() => _customErrorText = errorText);
198+
validate(clearCustomError: false);
190199
requestFocus();
191200
}
192201

193202
InputDecoration get decoration => widget.decoration.copyWith(
194-
errorText: widget.decoration.errorText ?? errorText ?? _customErrorText,
195-
);
203+
errorText: widget.decoration.errorText ?? errorText,
204+
);
196205
}

0 commit comments

Comments
 (0)