Better validators for logs

This commit is contained in:
Roland Geider
2026-05-25 12:33:39 +02:00
parent a7e30fcf8c
commit dff9ed559c
8 changed files with 91 additions and 14 deletions

View File

@@ -0,0 +1,41 @@
/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (c) 2026 wger Team
*
* wger Workout Manager is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
/// Validates an optional decimal text field.
///
/// Empty input is allowed and returns `null`. Non-empty input must be
/// parseable by [numberFormat]; otherwise an `enterValidNumber` error
/// message is returned.
String? validateOptionalDecimal(
String? text,
NumberFormat numberFormat,
BuildContext context,
) {
final trimmed = (text ?? '').trim();
if (trimmed.isEmpty) {
return null;
}
if (numberFormat.tryParse(trimmed) == null) {
return AppLocalizations.of(context).enterValidNumber;
}
return null;
}

View File

@@ -0,0 +1,15 @@
import 'package:wger/l10n/generated/app_localizations.dart';
/// Cross-field validation for a workout log entry, mirroring the
/// reachable part of the backend `WorkoutLog.clean()` rule: at least
/// one of [repetitions] or [weight] must be set.
String? validateWorkoutLogCrossField({
required num? repetitions,
required num? weight,
required AppLocalizations i18n,
}) {
if (repetitions == null && weight == null) {
return i18n.weightOrRepsRequired;
}
return null;
}

View File

@@ -1001,5 +1001,6 @@
"type": "String"
}
}
}
},
"weightOrRepsRequired": "Bitte Gewicht, Wiederholungen oder beides eingeben"
}

View File

@@ -1382,5 +1382,9 @@
"sessionExpired": "Your session has expired, please log in again",
"@sessionExpired": {
"description": "Snackbar message shown when the saved login token is no longer valid"
},
"weightOrRepsRequired": "Please enter a weight, repetitions, or both",
"@weightOrRepsRequired": {
"description": "Validation error when both weight and repetitions are empty in a workout log"
}
}

View File

@@ -21,6 +21,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:intl/intl.dart';
import 'package:logging/logging.dart';
import 'package:wger/helpers/consts.dart';
import 'package:wger/helpers/form_validators.dart';
import 'package:wger/helpers/i18n.dart';
import 'package:wger/helpers/number_input.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
@@ -234,12 +235,7 @@ class _RepetitionInputWidgetState extends ConsumerState<RepetitionInputWidget> {
}
widget.onChanged(_numberFormat.parse(text));
},
validator: (text) {
if (_numberFormat.tryParse(text ?? '') == null) {
return i18n.enterValidNumber;
}
return null;
},
validator: (text) => validateOptionalDecimal(text, _numberFormat, context),
),
),

View File

@@ -21,6 +21,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:intl/intl.dart';
import 'package:logging/logging.dart';
import 'package:wger/helpers/consts.dart';
import 'package:wger/helpers/form_validators.dart';
import 'package:wger/helpers/i18n.dart';
import 'package:wger/helpers/number_input.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
@@ -229,12 +230,7 @@ class _WeightInputWidgetState extends ConsumerState<WeightInputWidget> {
}
widget.onChanged(_numberFormat.parse(text));
},
validator: (text) {
if (_numberFormat.tryParse(text ?? '') == null) {
return i18n.enterValidNumber;
}
return null;
},
validator: (text) => validateOptionalDecimal(text, _numberFormat, context),
),
),

View File

@@ -21,6 +21,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:intl/intl.dart';
import 'package:logging/logging.dart';
import 'package:wger/helpers/consts.dart';
import 'package:wger/helpers/routines/validators.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/models/workouts/log.dart';
import 'package:wger/models/workouts/set_config_data.dart';
@@ -339,6 +340,16 @@ class _LogFormWidgetState extends ConsumerState<LogFormWidget> {
}
_form.currentState!.save();
final error = validateWorkoutLogCrossField(
repetitions: log.repetitions,
weight: log.weight,
i18n: i18n,
);
if (error != null) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(error)));
return;
}
final gymState = ref.read(gymStateProvider);
final gymProvider = ref.read(gymStateProvider.notifier);
final page = gymState.getSlotEntryPageByIndex()!;

View File

@@ -19,6 +19,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:wger/helpers/routines/validators.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/models/workouts/log.dart';
import 'package:wger/models/workouts/repetition_unit.dart';
@@ -84,6 +85,19 @@ class _LogEditDialogState extends ConsumerState<LogEditDialog> {
return;
}
_form.currentState!.save();
final i18n = AppLocalizations.of(context);
final error = validateWorkoutLogCrossField(
repetitions: _repetitions,
weight: _weight,
i18n: i18n,
);
if (error != null) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(error)));
return;
}
setState(() => _saving = true);
final updated = widget.log.copyWith(
@@ -94,7 +108,6 @@ class _LogEditDialogState extends ConsumerState<LogEditDialog> {
weightUnitObj: _weightUnit,
);
final i18n = AppLocalizations.of(context);
try {
await ref.read(workoutLogProvider).updateEntry(updated);
if (!mounted) {