Reputation: 21
So, this is my first ever question on Stack Overflow. Please be gentile..
I'm trying to use an IntegerField as a HiddenInput, but when I submit the form, the value of the field doesn't get sent to my app. (When I debug, the value is ''), but I can see that the proper value is set on the page itself via dev tools.
If I remove the widget=HiddenInput(), it works without issue. I'm sure there's some ridiculously obvious reason why, but I've been unable to find it.
The class:
class RemoveTimeslot(FlaskForm):
ts_id = IntegerField(widget=HiddenInput())
remove = SubmitField('Remove')
From the view:
slot = Timeslot.query.filter_by(id=rem_form.ts_id.data).first()
if slot:
db.session.delete(slot)
db.session.commit()
flash('Timeslot Removed!')
return redirect(url_for('admin.timeslots'))
else:
flash('Failed to remove timeslot!', 'warning')
From the template: '''
{% for slot in slots %}
<tr>
<td>{{slot.start.isoformat(timespec='minutes')}}</td>
<td>{{slot.end.isoformat(timespec='minutes')}}</td>
<td>{{slot.duration}} Minutes</td>
<td>
<form method="POST" action="">
{{ rem_form.hidden_tag() }}
{{ rem_form.remove(class="btn btn-dark") }}
{{ rem_form.ts_id(value=slot.id) }}
</form>
</td>
</tr>
{% endfor %}
Any help would be appreciated!
Upvotes: 1
Views: 513
Reputation: 21
I faced the same issue and after many hours in I was able to find the cause for this. The reason that HiddenFields not receiving the values passsed from the template is when you are having the Hidden form field after the
{{ form.hidden_tag() }}
If you add the hidden fields before this tag then it works fine!
Here's an example:
<form method="POST" action="">
{{ form.vehical_id(class="form-control", id="vehicalId") }}
{{ form.hidden_tag() }}
<fieldset class="form-group">
<div class="form-group position-relative">
{{ form.vehical.label(class="form-label mt-3", for="vehicalSearch") }}
{{ form.vehical(class="form-control", id="vehicalSearch", placeholder="Search Vehical...", autocomplete="off") }}
</div>
</fieldset>
</form>
Upvotes: 0
Reputation: 21
I was finally able to get it to work by adding type="hidden"
in the template. In the class itself, I left it as ts_id = IntegerField()
, but when I created the field in the template I changed it to {{ rem_form.ts_id(value=slot.id, type="hidden") }}
, and that worked! I don't know why one works differently than the other... let me know if you have any ideas!
Upvotes: 1