From 072adf959f45801d2a8179b7b71b83327b899171 Mon Sep 17 00:00:00 2001 From: xavierchanth Date: Thu, 14 Sep 2023 23:31:10 +0800 Subject: [PATCH] fix: profileName validator --- .../home_screen_import_dialog.dart | 53 ++++++++++++++----- .../sshnp_gui/lib/src/utility/constants.dart | 3 +- .../lib/src/utility/form_validator.dart | 4 +- 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/packages/sshnp_gui/lib/src/presentation/widgets/home_screen_actions/home_screen_import_dialog.dart b/packages/sshnp_gui/lib/src/presentation/widgets/home_screen_actions/home_screen_import_dialog.dart index 287f1c8f5..956b3082d 100644 --- a/packages/sshnp_gui/lib/src/presentation/widgets/home_screen_actions/home_screen_import_dialog.dart +++ b/packages/sshnp_gui/lib/src/presentation/widgets/home_screen_actions/home_screen_import_dialog.dart @@ -1,39 +1,66 @@ import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; +import 'package:sshnp_gui/src/presentation/widgets/profile_form/custom_text_form_field.dart'; +import 'package:sshnp_gui/src/utility/form_validator.dart'; -class HomeScreenImportDialog extends StatelessWidget { +class HomeScreenImportDialog extends StatefulWidget { final void Function(String?) setValue; + final String? initialName; - const HomeScreenImportDialog(this.setValue, {this.initialName, Key? key}) : super(key: key); + const HomeScreenImportDialog(this.setValue, {this.initialName, Key? key}) + : super(key: key); + + @override + State createState() => _HomeScreenImportDialogState(); +} + +class _HomeScreenImportDialogState extends State { + final GlobalKey _formkey = GlobalKey(); + String? result; @override Widget build(BuildContext context) { - final TextEditingController controller = TextEditingController(text: initialName); final strings = AppLocalizations.of(context)!; - return AlertDialog( title: Text(strings.importProfile), - content: TextField( - controller: controller, - decoration: InputDecoration(hintText: strings.profileName), + content: Form( + key: _formkey, + child: CustomTextFormField( + initialValue: widget.initialName, + labelText: strings.profileName, + onChanged: (value) { + result = value; + }, + validator: FormValidator.validateProfileNameField, + ), ), actions: [ OutlinedButton( - onPressed: () => Navigator.of(context).pop(false), + onPressed: () { + Navigator.of(context).pop(false); + }, child: Text(strings.cancelButton, - style: Theme.of(context).textTheme.bodyLarge!.copyWith(decoration: TextDecoration.underline)), + style: Theme.of(context) + .textTheme + .bodyLarge! + .copyWith(decoration: TextDecoration.underline)), ), ElevatedButton( - onPressed: () async { - setValue(controller.text); - if (context.mounted) Navigator.of(context).pop(); + onPressed: () { + if (_formkey.currentState!.validate()) { + widget.setValue(result); + Navigator.of(context).pop(); + } }, style: Theme.of(context).elevatedButtonTheme.style!.copyWith( backgroundColor: MaterialStateProperty.all(Colors.black), ), child: Text( strings.submit, - style: Theme.of(context).textTheme.bodyLarge!.copyWith(fontWeight: FontWeight.w700, color: Colors.white), + style: Theme.of(context) + .textTheme + .bodyLarge! + .copyWith(fontWeight: FontWeight.w700, color: Colors.white), ), ) ], diff --git a/packages/sshnp_gui/lib/src/utility/constants.dart b/packages/sshnp_gui/lib/src/utility/constants.dart index cf5b2bba1..dd2457eef 100644 --- a/packages/sshnp_gui/lib/src/utility/constants.dart +++ b/packages/sshnp_gui/lib/src/utility/constants.dart @@ -7,7 +7,8 @@ const kBackGroundColorDark = Color(0xFF222222); const kEmptyFieldValidationError = 'Field cannot be left blank'; const kAtsignFieldValidationError = 'Field must start with @'; -const kProfileNameFieldValidationError = 'Field cannot contain any of ".@:_"'; +const kProfileNameFieldValidationError = + 'Field must only use alphanumeric characters and spaces'; const String dotEnvMimeType = 'text/plain'; const XTypeGroup dotEnvTypeGroup = XTypeGroup( diff --git a/packages/sshnp_gui/lib/src/utility/form_validator.dart b/packages/sshnp_gui/lib/src/utility/form_validator.dart index b8051c5eb..2e654333d 100644 --- a/packages/sshnp_gui/lib/src/utility/form_validator.dart +++ b/packages/sshnp_gui/lib/src/utility/form_validator.dart @@ -18,10 +18,10 @@ class FormValidator { } static String? validateProfileNameField(String? value) { - String invalidChars = '.@:_'; + String invalid = '[^a-zA-Z0-9 ]'; if (value?.isEmpty ?? true) { return kEmptyFieldValidationError; - } else if (value!.contains(RegExp('[$invalidChars]'))) { + } else if (value!.contains(RegExp(invalid))) { return kProfileNameFieldValidationError; } return null;