Skip to content

Commit

Permalink
fix: profileName validator
Browse files Browse the repository at this point in the history
  • Loading branch information
XavierChanth committed Sep 14, 2023
1 parent aa0c591 commit 072adf9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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<HomeScreenImportDialog> createState() => _HomeScreenImportDialogState();
}

class _HomeScreenImportDialogState extends State<HomeScreenImportDialog> {
final GlobalKey<FormState> _formkey = GlobalKey<FormState>();
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),
),
)
],
Expand Down
3 changes: 2 additions & 1 deletion packages/sshnp_gui/lib/src/utility/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions packages/sshnp_gui/lib/src/utility/form_validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 072adf9

Please sign in to comment.