OpenNutriTracker icon indicating copy to clipboard operation
OpenNutriTracker copied to clipboard

fix: food name field validation

Open LeticiaMonteiroo opened this issue 5 months ago • 0 comments

Pull Request: Meal Name Validation

Description

This PR fixes the “Meal Name” field validation in OpenNutriTracker, preventing inputs made up solely of numbers or special characters, and ensuring at least one alphabetical character is present.

What Was Done

  • Input Validation
    Created and tested FoodNameValidator.isValid, which enforces at least one letter in the meal name.

  • Form Integration
    Wrapped name and related fields in a Form using a GlobalKey<FormState> and added a validator to the meal name TextFormField.

  • Submission Blocking
    Updated _onSavePressed to call formKey.currentState!.validate() before proceeding, preventing invalid submissions.

  • Test Coverage

    • Unit Tests rejecting number‑only or symbol‑only names and accepting letter‑containing names.

Linked Issues

Bug: App allows adding a meal with only numbers and special characters in the name #211

TDD Cycles

  1. Red

    • Wrote failing unit tests for the new validation scenarios.
  2. Green‑1

    • Introduced a stub (return false) and fixed imports so tests compile (but still fail).
  3. Green‑2

    • Implemented real logic with RegExp(r'[A-Za-z]'), making all tests pass.
  4. Refactor & Integrate

    • Applied the validated logic in the form and ensured submission is blocked on error.

Outcome

  • All tests pass successfully.
  • The UI displays “The name must contain at least one letter” and prevents saving until corrected.

LeticiaMonteiroo avatar Jul 11 '25 23:07 LeticiaMonteiroo