Skip to content

Commit e18e7b2

Browse files
committed
login: Make username/password fields required
Fixes-partly: zulip#35
1 parent dc9d644 commit e18e7b2

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

lib/widgets/login.dart

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,15 @@ class _EmailPasswordLoginPageState extends State<EmailPasswordLoginPage> {
119119
void _submit() async {
120120
final context = _emailKey.currentContext!;
121121
final realmUrl = widget.serverSettings.realmUri;
122-
final String? email = _emailKey.currentState!.value;
123-
final String? password = _passwordKey.currentState!.value;
124-
if (email == null || password == null) {
125-
// TODO can these FormField values actually be null? when?
122+
final emailFieldState = _emailKey.currentState!;
123+
final passwordFieldState = _passwordKey.currentState!;
124+
final emailValid = emailFieldState.validate(); // Side effect: on-field error text
125+
final passwordValid = passwordFieldState.validate(); // Side effect: on-field error text
126+
if (!emailValid || !passwordValid) {
126127
return;
127128
}
128-
// TODO(#35): validate email is in the shape of an email
129+
final String email = emailFieldState.value!;
130+
final String password = passwordFieldState.value!;
129131

130132
final FetchApiKeyResult result;
131133
setState(() {
@@ -194,6 +196,14 @@ class _EmailPasswordLoginPageState extends State<EmailPasswordLoginPage> {
194196
key: _emailKey,
195197
autofillHints: const [AutofillHints.email],
196198
keyboardType: TextInputType.emailAddress,
199+
autovalidateMode: AutovalidateMode.onUserInteraction,
200+
validator: (value) {
201+
if (value == null || value.trim().isEmpty) {
202+
return 'Please enter your email.';
203+
}
204+
// TODO(#35): validate is in the shape of an email
205+
return null;
206+
},
197207
decoration: const InputDecoration(
198208
labelText: 'Email address')),
199209
const SizedBox(height: 8),
@@ -202,6 +212,13 @@ class _EmailPasswordLoginPageState extends State<EmailPasswordLoginPage> {
202212
autofillHints: const [AutofillHints.password],
203213
obscureText: _obscurePassword,
204214
keyboardType: TextInputType.visiblePassword,
215+
autovalidateMode: AutovalidateMode.onUserInteraction,
216+
validator: (value) {
217+
if (value == null || value.isEmpty) {
218+
return 'Please enter your password.';
219+
}
220+
return null;
221+
},
205222
decoration: InputDecoration(
206223
labelText: 'Password',
207224
suffixIcon: Semantics(label: 'Hide password', toggled: _obscurePassword,

0 commit comments

Comments
 (0)