Add some validation to the exercise form

This commit is contained in:
Roland Geider
2022-03-28 23:36:46 +02:00
parent 6214240ea0
commit c4d26b3f01
2 changed files with 61 additions and 10 deletions

View File

@@ -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",

View File

@@ -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<AddExerciseScreen> {
int _currentStep = 0;
int lastStepIndex = AddExerciseScreen.STEPS_IN_FORM - 1;
@@ -86,15 +92,15 @@ class _AddExerciseScreenState extends State<AddExerciseScreen> {
),
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<AddExerciseProvider>();
final exerciseProvider = context.read<ExercisesProvider>();
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!,
),
],