From c4d26b3f01709df1b0d09506981ddebfac8ebabe Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Mon, 28 Mar 2022 23:36:46 +0200 Subject: [PATCH] Add some validation to the exercise form --- lib/l10n/app_en.arb | 8 ++++ lib/screens/add_exercise_screen.dart | 63 +++++++++++++++++++++++----- 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 17efed32..09949ead 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -477,6 +477,14 @@ "max": {} } }, + "enterMinCharacters": "Please enter at least {min} characters", + "@enterMinCharacters": { + "description": "Error message when the user hasn't entered the minimum amount characters in a form", + "type": "text", + "placeholders": { + "min": {} + } + }, "nrOfSets": "Sets per exercise: {nrOfSets}", "@nrOfSets": { "description": "Label shown on the slider where the user selects the nr of sets", diff --git a/lib/screens/add_exercise_screen.dart b/lib/screens/add_exercise_screen.dart index b11dacde..8fb6f546 100644 --- a/lib/screens/add_exercise_screen.dart +++ b/lib/screens/add_exercise_screen.dart @@ -31,6 +31,12 @@ abstract class ValidateStep { abstract VoidCallback _submit; } +/// The amount of characters an exercise description needs to have +const MIN_CHARACTERS_DESCRIPTION = 40; + +/// The amount of characters an exercise name needs to have +const MIN_CHARACTERS_NAME = 6; + class _AddExerciseScreenState extends State { int _currentStep = 0; int lastStepIndex = AddExerciseScreen.STEPS_IN_FORM - 1; @@ -86,15 +92,15 @@ class _AddExerciseScreenState extends State { ), Step( title: Text(AppLocalizations.of(context).description), - content: _DescriptionStepContent(formkey: _keys[3]), + content: _DescriptionStepContent(formkey: _keys[2]), ), Step( title: Text(AppLocalizations.of(context).translation), - content: _DescriptionTranslationStepContent(formkey: _keys[4]), + content: _DescriptionTranslationStepContent(formkey: _keys[3]), ), Step( title: Text(AppLocalizations.of(context).images), - content: _ImagesStepContent(formkey: _keys[2]), + content: _ImagesStepContent(formkey: _keys[4]), ), ], currentStep: _currentStep, @@ -157,7 +163,17 @@ class _BasicStepContent extends StatelessWidget { onChange: (value) => {}, title: '${AppLocalizations.of(context).name}*', isRequired: true, - validator: (name) => name?.isEmpty ?? true ? 'Name is required' : null, + validator: (name) { + if (name!.isEmpty) { + return AppLocalizations.of(context).enterValue; + } + + if (name.length < MIN_CHARACTERS_NAME) { + return AppLocalizations.of(context).enterMinCharacters(MIN_CHARACTERS_NAME); + } + + return null; + }, onSaved: (String? name) => addExerciseProvider.exerciseNameEn = name!, ), AddExerciseTextArea( @@ -292,9 +308,6 @@ class _DescriptionStepContent extends StatelessWidget { @override Widget build(BuildContext context) { final addExerciseProvider = context.read(); - final exerciseProvider = context.read(); - - final languages = exerciseProvider.languages; return Form( key: formkey, @@ -305,7 +318,17 @@ class _DescriptionStepContent extends StatelessWidget { title: '${AppLocalizations.of(context).description}*', isRequired: true, isMultiline: true, - validator: (name) => name?.isEmpty ?? true ? 'Name is required' : null, + validator: (name) { + if (name!.isEmpty) { + return AppLocalizations.of(context).enterValue; + } + + if (name.length < MIN_CHARACTERS_DESCRIPTION) { + return AppLocalizations.of(context).enterMinCharacters(MIN_CHARACTERS_DESCRIPTION); + } + + return null; + }, onSaved: (String? description) => addExerciseProvider.descriptionEn = description!, ), ], @@ -341,7 +364,17 @@ class _DescriptionTranslationStepContent extends StatelessWidget { onChange: (value) => {}, title: '${AppLocalizations.of(context).name}*', isRequired: true, - validator: (name) => name?.isEmpty ?? true ? 'Name is required' : null, + validator: (name) { + if (name!.isEmpty) { + return AppLocalizations.of(context).enterValue; + } + + if (name.length < MIN_CHARACTERS_NAME) { + return AppLocalizations.of(context).enterMinCharacters(MIN_CHARACTERS_NAME); + } + + return null; + }, onSaved: (String? name) => addExerciseProvider.exerciseNameTrans = name!, ), AddExerciseTextArea( @@ -357,7 +390,17 @@ class _DescriptionTranslationStepContent extends StatelessWidget { title: '${AppLocalizations.of(context).description}*', isRequired: true, isMultiline: true, - validator: (name) => name?.isEmpty ?? true ? 'Name is required' : null, + validator: (name) { + if (name!.isEmpty) { + return AppLocalizations.of(context).enterValue; + } + + if (name.length < MIN_CHARACTERS_DESCRIPTION) { + return AppLocalizations.of(context).enterMinCharacters(MIN_CHARACTERS_DESCRIPTION); + } + + return null; + }, onSaved: (String? description) => addExerciseProvider.descriptionTrans = description!, ), ],