最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - wtforms: optional FieldList of IntegerField? - Stack Overflow

programmeradmin4浏览0评论

I need an optional list of int's in a WTForms-validated Rest API.

Right now this is the relevant part of my form:

from wtforms import IntegerField, FileField, StringField, FieldList
from wtforms.validators import DataRequired, Optional, UUID

class PublishForm(FlaskForm):
    # This validates, but the patch_bases list comes up empty.
    # patch_bases = FieldList(IntegerField("pb", validators=[Required()]), min_entries=0, max_entries=5, validators=[Optional()])
    # This too.
    patch_bases = FieldList(IntegerField(), min_entries=0, max_entries=5, validators=[Optional()])

Here's the call to validate/parse:

form = PublishForm()
if not form.validate_on_submit():
    return form_error_response(form.errors)

And here's (part of) a test curl I'm using to hit the API:

curl \
    -v \
    -X POST \
    -H "Authorization: Bearer $(set -eu; cat token)" \
    --form 'patch_bases=[2, 4]' \
    http://hostname:8080/api/v3/publish

I'm using --form because I need to upload a file as well (not shown here).

I'm consistently getting back a "successfully validated" empty list even when I pass a nonempty list of int's via curl, which is not what I want.

What I want, ideally, is a None for no patch_bases field specified at all, an empty list for patch_bases=[], or a nonempty list for 1 to 5 int's in patch_bases - all in form.patch_bases.data.

I've Googled about this extensively on more than one occasion, but I'm not seeing a way of doing what I want.

I'm using:

WTForms                 2.0.2
Python                  3.10.12
Flask                   1.1.2

I know, the WTForms version is ancient, but I'm stuck with it for now unless there's a pretty compelling reason to upgrade.

If there's a fine manual to read, feel free to point me at it? I dug around in the WTForms 2.3.x doc on Fields (among many other Google hits), but didn't see what I need.

Thanks!

PS: I just tried the MultivalueOptional class from - but field.raw_data is coming up None even when I do pass a value.

I need an optional list of int's in a WTForms-validated Rest API.

Right now this is the relevant part of my form:

from wtforms import IntegerField, FileField, StringField, FieldList
from wtforms.validators import DataRequired, Optional, UUID

class PublishForm(FlaskForm):
    # This validates, but the patch_bases list comes up empty.
    # patch_bases = FieldList(IntegerField("pb", validators=[Required()]), min_entries=0, max_entries=5, validators=[Optional()])
    # This too.
    patch_bases = FieldList(IntegerField(), min_entries=0, max_entries=5, validators=[Optional()])

Here's the call to validate/parse:

form = PublishForm()
if not form.validate_on_submit():
    return form_error_response(form.errors)

And here's (part of) a test curl I'm using to hit the API:

curl \
    -v \
    -X POST \
    -H "Authorization: Bearer $(set -eu; cat token)" \
    --form 'patch_bases=[2, 4]' \
    http://hostname:8080/api/v3/publish

I'm using --form because I need to upload a file as well (not shown here).

I'm consistently getting back a "successfully validated" empty list even when I pass a nonempty list of int's via curl, which is not what I want.

What I want, ideally, is a None for no patch_bases field specified at all, an empty list for patch_bases=[], or a nonempty list for 1 to 5 int's in patch_bases - all in form.patch_bases.data.

I've Googled about this extensively on more than one occasion, but I'm not seeing a way of doing what I want.

I'm using:

WTForms                 2.0.2
Python                  3.10.12
Flask                   1.1.2

I know, the WTForms version is ancient, but I'm stuck with it for now unless there's a pretty compelling reason to upgrade.

If there's a fine manual to read, feel free to point me at it? I dug around in the WTForms 2.3.x doc on Fields (among many other Google hits), but didn't see what I need.

Thanks!

PS: I just tried the MultivalueOptional class from https://github/pallets-eco/wtforms/issues/835 - but field.raw_data is coming up None even when I do pass a value.

Share Improve this question edited yesterday dstromberg asked yesterday dstrombergdstromberg 7,1872 gold badges30 silver badges29 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I wound up skipping wtforms for this field, since wtforms is not really intended to be a general-purpose validation library.

Instead, I'm using request.form.get("patch_bases") to get back a str that contains a list of int's. Pass that to ast.literal_eval(string), and things are good. :)

request.form is an ordered dict of key-value pairs extracted from the body of the API call.

发布评论

评论列表(0)

  1. 暂无评论