Reputation: 2769
all. I am working on a Flask application. I am developing a website for a restaurant with an online ordering system. I'm doing this for a friend and taking it as a learning experience. The issue I am going to ask about is probably a small one. I am not sure whether the issue is more with wtforms or flask-wtf. Hopefully someone can provide some insight.
I am creating a shopping cart, with a few buttons that are rendered through wtf.quick_form
. Here is that:
<div id='cart'>
<div>
<p>
<center><b>Shopping cart</b></center>
</p>
<div>
{% for item in requested_item_record %}
<div id='cartI'>{{list_of_items[item]['name']}}</div>
<div id='cartB'> {{ wtf.quick_form(remove_item_dict[item]) }}</div>
{% endfor %}
</div>
<p id='cartT'>Subtotal: {{ total_cost/100 }} </p>
<p id='cartT'>Tax: {{ tax/100 }}</p>
<p id='cartT'>Total: {{ total/100 }}</p>
<div id='cartB'>{{ wtf.quick_form(clear_cart) }}</div>
<div id='cartB'>{{ wtf.quick_form(order_type) }}</div>
</div>
</div>
Here are the buttons defined by wtforms
:
class AddItem(Form):
submit = SubmitField("Add item to cart")
class ClearCart(Form):
submit = SubmitField("Clear cart")
class RemoveItem(Form):
submit = SubmitField("Remove item")
class OrderType(Form):
select = SelectField("Pickup or delivery?", choices=[('pickup','Pickup'),('delivery','Delivery')])
submit = SubmitField("Proceed to checkout")
The problem is that I am seeing "Not a valid choice" below the SelectField
from the OrderType
button. I think this will confuse users. I am not sure how to remove it though. Here is a screenshot:
I only see the behavior if I submit data with one of the RemoveItem
buttons or the ClearCart
button.
Here is the relevant portion of my view function:
# generate add buttons for all items of the menu
button_dict = OrderedDict([])
for i in range(number_of_submit_buttons):
button_dict[i] = AddItem(prefix='add item ' + str(i))
clear_cart = ClearCart(prefix="clear cart")
# check for submit data from users, remove an item or clear cart.
for iterable, add_button in enumerate(button_dict):
if button_dict[add_button].validate_on_submit and button_dict[add_bu$
session['requested_item_record'].append(iterable)
session['total_cost'] += list_of_items[iterable]['price']
if clear_cart.validate_on_submit and clear_cart.submit.data:
session['requested_item_record'] = []
session['total_cost'] = 0
# select field, for delivery or pickup
order_type = OrderType(prefix='order_type')
if order_type.validate_on_submit() and order_type.submit.data:
if order_type.select.data == 'delivery' and session['requested_i$
return redirect(url_for('transaction.get_address'))
elif order_type.select.data == 'pickup' and session['requested_i$
return redirect(url_for('transaction.checkout'))
The functionality is fine. The website works how I want it to, just for some reason I am getting this error of "Not a valid choice". This appears only when I use one of the remove item or clear cart buttons.
Upvotes: 1
Views: 4915
Reputation: 67489
The problem is, I think, in this line:
if order_type.validate_on_submit() and order_type.submit.data:
Here you unconditionally validate the OrderType
form, so any time you click on the other buttons the validation for this form is going to fail due to the select drop down not having a value.
Try reversing the two parts of the conditional, as follows:
if order_type.submit.data and order_type.validate_on_submit():
In this way, the validation will only be done when the submit button for that form has value, which indicates that that's the form that was submitted by the user.
Upvotes: 1