flutter icon indicating copy to clipboard operation
flutter copied to clipboard

fixed ingredient weight validation

Open dhituval opened this issue 1 month ago • 0 comments

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. Screenshot 2025-12-02 at 4 03 50 PM

Proposed Changes

  • Updated IngredientForm in lib/widgets/nutrition/forms.dart:
    • Strengthened the validator for the field-weight TextFormField.
    • 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.
  • Kept the existing form flow, so only valid values reach NutritionPlansProvider.addMealItem and the backend no longer throws a validation exception.

Testing

Manual testing on iOS simulator:

  1. Original failing case:

    • Open Nutrition → select a plan → add ingredient to a meal.
    • Select an ingredient and enter 0.5 in 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.
  2. Valid integer weight:

    • Enter 50 in 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.
  3. Other invalid inputs:

    • Empty value → shows “Please enter a weight.”
    • Non-numeric value (e.g., abc) → shows “Enter a valid number.”
    • 0 or 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.

dhituval avatar Dec 02 '25 21:12 dhituval