Fix mistake in dish change detection
When the form is submitted, the URL has no dish parameter any more. To fix this, add a hidden form field to say which dish the form was for.
This commit is contained in:
parent
94d4a403cb
commit
b8ac141310
2 changed files with 6 additions and 4 deletions
|
@ -54,6 +54,7 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% if dish and dish.choices %}
|
{% if dish and dish.choices %}
|
||||||
|
<input type="hidden" name="form_for_dish" value="{{ dish.id }}" />
|
||||||
{% for (choice_type, choice) in dish.choices %}
|
{% for (choice_type, choice) in dish.choices %}
|
||||||
<div class="form-group select2-container select2">
|
<div class="form-group select2-container select2">
|
||||||
<label class="control-label" for="choice_{{ choice.id }}">{{ choice.name }}</label><br/>
|
<label class="control-label" for="choice_{{ choice.id }}">{{ choice.name }}</label><br/>
|
||||||
|
|
|
@ -126,15 +126,16 @@ def order_item_create(order_id: int) -> typing.Any:
|
||||||
form = AnonOrderItemForm() if current_user.is_anonymous() \
|
form = AnonOrderItemForm() if current_user.is_anonymous() \
|
||||||
else OrderItemForm()
|
else OrderItemForm()
|
||||||
|
|
||||||
dish_id_in_url = request.args.get("dish")
|
dish_id = form.dish_id.data if form.is_submitted() else request.args.get("dish")
|
||||||
dish_id = form.dish_id.data if form.is_submitted() else dish_id_in_url
|
|
||||||
if dish_id and not location.dish_by_id(dish_id):
|
if dish_id and not location.dish_by_id(dish_id):
|
||||||
abort(404)
|
abort(404)
|
||||||
form.dish_id.data = dish_id
|
form.dish_id.data = dish_id
|
||||||
form.populate(current_order.location, dish_id)
|
form.populate(current_order.location, dish_id)
|
||||||
|
|
||||||
# If the form was not submitted (GET request), the form had errors, or the dish was changed: show form again
|
# If the form was not submitted (GET request), the form had errors,
|
||||||
if not form.validate_on_submit() or (dish_id_in_url and dish_id_in_url != dish_id):
|
# or the dish was changed: show form again
|
||||||
|
dish_was_changed = request.form.get("form_for_dish_id") and request.form["form_for_dish_id"] != dish_id
|
||||||
|
if not form.validate_on_submit() or dish_was_changed:
|
||||||
return order_from_id(order_id, form=form, dish_id=dish_id)
|
return order_from_id(order_id, form=form, dish_id=dish_id)
|
||||||
|
|
||||||
# Form was submitted and is valid
|
# Form was submitted and is valid
|
||||||
|
|
Loading…
Reference in a new issue