fixed ingredient weight validation
Fixes wger-project/flutter#721
Summary
When adding a new ingredient to a meal or logging it to the diary, entering a
decimal weight less than 1 (for example 0.5) caused the backend to reject the
request with the validation error “Ensure this value is greater than or equal
to 1.” The app did not handle this case and the page was popped without saving
the ingredient, which matched the bug report in #721.
This PR adds client-side validation to the weight field in the nutrition
ingredient form so invalid values are rejected before the request is sent, and
the user sees a clear error message instead of the page closing.
Proposed Changes
- Updated
IngredientForminlib/widgets/nutrition/forms.dart:- Strengthened the validator for the
field-weightTextFormField. - The field now:
- requires a value,
- checks that the input parses as a number,
- enforces
weight >= 1, - and requires a whole number (no decimal weights).
- If validation fails, an inline error message is shown under the weight field and the form is not submitted.
- Strengthened the validator for the
- Kept the existing form flow, so only valid values reach
NutritionPlansProvider.addMealItemand the backend no longer throws a validation exception.
Testing
Manual testing on iOS simulator:
-
Original failing case:
- Open Nutrition → select a plan → add ingredient to a meal.
- Select an ingredient and enter
0.5in the weight field. - Press Save.
- Result: Page stays open and an error appears under the weight field. No exception is thrown and no request is sent.
-
Valid integer weight:
- Enter
50in the weight field and press Save. - Result: Form submits successfully, the page closes, and the ingredient is added as expected. No errors in the console.
- Enter
-
Other invalid inputs:
- Empty value → shows “Please enter a weight.”
- Non-numeric value (e.g.,
abc) → shows “Enter a valid number.” 0or other values< 1→ shows “Weight must be at least 1.”1.5→ shows “Please enter a whole number.”
Related Issue(s)
#721
If applicable, please link to any related issues (Closes #123,
Closes wger-project/other-repo#123, See also #123, etc.)
Please check that the PR fulfills these requirements
- [ ] Tests for the changes have been added (for bug fixes / features)
- [ ] Set a 100 character limit in your editor/IDE to avoid white space diffs in the PR
(run
dart format .) - [ ] Updated/added relevant documentation (doc comments with
///). - [ ] Added relevant reviewers.