fix issue with price when not valid float
Fixes #13 (closed)
We now properly catch the error and flash the message without continuing otherwise we continue like normal.
we also check if form was submitted by looking for "submit" in request.params and inside of making it disabled with make it not show in the request.params we instead hide the button on first submit to avoid people hammering the submit button.
Summary: This commit addresses two main issues:
Properly handling invalid price inputs by flashing an error message and preventing further processing.
Preventing multiple form submissions by hiding the submit button on the first submit, ensuring the "submit" parameter remains in request.params.
Details:
Price Validation:
Added a try-except block to catch ValueError when converting the price to a float.
If the price is not a valid float, an error message "Please enter a valid number for price." is flashed, and further processing is halted.
Ensured that the price validation occurs only if all required fields (title, description, and price) are provided.
Preventing Multiple Submissions:
Modified the form's onsubmit attribute to hide the submit button instead of disabling it. This prevents the "submit" parameter from being removed from request.params.
This change ensures that the form submission is processed correctly and prevents users from hammering the submit button.
Changes:
View Function (product_new):
Added a check for the "submit" parameter in request.params to determine if the form was submitted.
Added a check to ensure all required fields (title, description, and price) are provided. If any field is missing, an error message "You must fill out all fields." is flashed.
Added a try-except block to validate the price. If the price is not a valid float, an error message is flashed, and further processing is halted.
Ensured that the product creation logic is only executed if all fields are valid and the price is correctly formatted.
Template (product_new.j2):
Modified the form's onsubmit attribute to hide the submit button on the first submit, preventing multiple submissions while keeping the "submit" parameter in request.params.
Testing:
Updated functional tests to ensure that the error messages are correctly flashed and displayed when required fields are missing or the price is invalid.
Verified that the form submission is processed correctly and that the submit button is hidden on the first submit to prevent multiple submissions.
modified: make_post_sell/templates/product_new.j2
modified: make_post_sell/tests/test_functional.py
modified: make_post_sell/views/product.py
Summary by CodeRabbit
-
New Features
- Improved form submission in product creation by hiding the submit button upon submission to prevent multiple submissions.
-
Bug Fixes
- Enhanced validation for required fields and price input during product creation.
- Updated error handling and message flashing for better user feedback.
-
Tests
- Added new test case to ensure proper behavior of the submit button during product submission.
Edited by CodeRabbit