From 5ed6e3b614614c81376aac0690aaf5a4304ff347 Mon Sep 17 00:00:00 2001 From: Monika Theiss Date: Thu, 17 Oct 2024 07:19:49 +0200 Subject: [PATCH] added validation to edit profile and a max length to the username input on the home page --- .../components/settings/edit_input_text.dart | 21 +++++++++++++++---- lib/view/pages/home/home_page.dart | 1 + .../pages/settings/edit_profile_page.dart | 12 +++++++---- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/lib/view/components/settings/edit_input_text.dart b/lib/view/components/settings/edit_input_text.dart index 17096f32..81401664 100644 --- a/lib/view/components/settings/edit_input_text.dart +++ b/lib/view/components/settings/edit_input_text.dart @@ -4,16 +4,18 @@ class EditInputText extends StatefulWidget { final String label; final void Function(String value) onChanged; final String input; - final int maxLines; + final int maxLength; final Key inputKey; + final bool validate; const EditInputText( {super.key, - required this.maxLines, + required this.maxLength, required this.label, required this.onChanged, required this.input, - required this.inputKey}); + required this.inputKey, + required this.validate}); @override State createState() => _EditInputText(); @@ -56,7 +58,18 @@ class _EditInputText extends State { ), initialValue: widget.input, style: TextStyle(color: Theme.of(context).colorScheme.secondary, fontSize: 12), - maxLines: widget.maxLines, + maxLength: widget.maxLength, + validator: widget.validate?(value) { + if (value == null || value.isEmpty) { + return 'Please enter a username'; + } + if (!RegExp(r'^[a-zA-Z ]+$').hasMatch(value)) { + return 'Only alphabetical letters are permitted'; + } + return null; + }: (value){ + return null; + }, ), ], ), diff --git a/lib/view/pages/home/home_page.dart b/lib/view/pages/home/home_page.dart index ab5c070b..e3b60267 100644 --- a/lib/view/pages/home/home_page.dart +++ b/lib/view/pages/home/home_page.dart @@ -134,6 +134,7 @@ class _HomePageState extends State { content: Form( key: _formKey, child: TextFormField( + maxLength: 16, key: Key('usernameInput'), autofocus: true, controller: usernamecontroller, diff --git a/lib/view/pages/settings/edit_profile_page.dart b/lib/view/pages/settings/edit_profile_page.dart index 9d79a469..fcaa3715 100644 --- a/lib/view/pages/settings/edit_profile_page.dart +++ b/lib/view/pages/settings/edit_profile_page.dart @@ -359,10 +359,11 @@ class _EditProfilePage extends State { children: [ EditInputText( inputKey: const Key('usernameField'), - maxLines: 1, + maxLength: 16, label: 'Username', onChanged: (value) => _username = value, input: _username, + validate: true, ), Row( mainAxisAlignment: @@ -371,28 +372,31 @@ class _EditProfilePage extends State { Flexible( child: EditInputText( inputKey: const Key('firstNameField'), - maxLines: 1, + maxLength: 16, label: 'First name', onChanged: (value) => _firstName = value, input: _firstName, + validate: true, ), ), Flexible( child: EditInputText( inputKey: const Key('lastNameField'), - maxLines: 1, + maxLength: 16, label: 'Last Name', onChanged: (value) => _lastName = value, input: _lastName, + validate: true, ), ), ]), EditInputText( inputKey: const Key('bioField'), - maxLines: 5, + maxLength: 100, label: 'Bio', onChanged: (value) => _bio = value, input: _bio, + validate: false, ), EditDateInput( currentDate: _birthday!,