简体   繁体   English

WTforms IntegerField in fieldlist从不使用手动迭代进行验证

[英]WTforms IntegerField in fieldlist never validates using manual iteration

I have an InterField , that validates if a number is between the values 0 and 99 . 我有一个InterField ,验证数字是否介于值099 For some reason it never validates. 由于某种原因,它永远不会验证。 I have a feeling it is related to the FieldList and ther way I iterate over it in the template, but can't seem to get it working. 我有一种感觉它与FieldList和我在模板中迭代它的方式相关,但似乎无法让它工作。

The form: 表格:

class dpiaImpactAnalysisForm(Form):

    severity_score = IntegerField("Severity Score"), 
        validators=[NumberRange(min=0, max=99, message="Please provide a valid number")]))
    identifiability_score = IntegerField("Identifiability Score"),
        validators=[NumberRange(min=0, max=99, message="Please provide a valid number")]))


class dpiaThreatAnalysisForm(Form):

    impact = FieldList(FormField(dpiaImpactAnalysisForm), min_entries=1)

In views I append the entries dynamically as required: 在视图中,我根据需要动态追加条目:

@app.route('/dpia/analysis/<project_id>', methods=["GET", "POST"])
def analysis(project_id):

    form = dpiaThreatAnalysisForm()

    prim = Assets.query.filter_by(selected=True, primary=True).all()
    primary_assets = list(map(vars, prim))

    ev = Events.query.all()
    events = list(map(vars, ev))

    # add fields to the form...
    for z in range(len(prim) * len(ev)):
        form.impact.append_entry()

    supp = Assets.query.filter_by(selected=True, primary=False).all()
    supporting_assets = list(map(vars, supp))

    ths = Threats.query.all()
    threats = list(map(vars, ths))

    # add fields to the form
    for z in range(len(ths) * len(supp)):
        form.likelihood.append_entry()

    if form.is_submitted():
        print "submitted"
    if form.validate():
        print "valid"
    print form.errors

    if form.validate_on_submit():
        # This is never printed:
        app.logger.info("success!!")
        pprint(form.likelihood)

    return redirect(url_for(next_step, project_id=project_id))
return render_template('analysis.html', form=form, threats=threats, supporting_assets=supporting_assets, primary_assets=primary_assets, events=events)

In the template I iterate over a list primary_assets in a list events , and add the fields per iteration: 在模板中,我迭代列表events的列表primary_assets ,并在每次迭代时添加字段:

{% for val in events %}
   {% if not counter or loop.index0 == 0 %}
      {% set counter = [] %}  <!-- loop hack !-->
   {% endif %}
   <strong>Event: {{ val.name }}</strong><br />
   Jeopardizes: {{ val.jeopardizes }}<br />

   {% for pa in primary_assets %}
       <strong>{{ pa['name'] }}</strong><br />
       {{ form.impact[counter|length].identifiability_score(placeholder='') }} <br />
       {{ form.impact[counter|length].severity_score(placeholder='') }} 
       {{ form.impact[counter|length].hidden_tag() }} 
       {% if counter.append('1') %}{% endif %}
   {% endfor %}
{% endfor %}

The hidden_tag() doesn't work either. hidden_tag()也不起作用。 Normally I iterate of the forms with with something like 通常我会使用类似的东西迭代表单

 {% for impact in form.impact %}
     {{ impact.form.hidden_tag() }}
     # do cbg
  {% endfor %}

and that works, that's why I believe it's my manual looping that spoils it... 这是有效的,这就是为什么我认为这是我的手动循环,破坏它...

EDIT 2 march, 17:26 编辑2月3日17:26

After some testing, I found that using 经过一些测试,我发现使用了

severity_score = IntegerField("Severity Score", validators=[Optional(), NumberRange( min=0, max=9999999999, message="Please provide a valid number")])

works (if I set min=50 I get an error when inserting a number below 50), however, the CSRF is still not getting passed. 工作(如果我设置min=50我在插入低于50的数字时会收到错误),但是,CSRF仍未通过。

{{ form.impact[counter|length].hidden_tag() }} or {{ form.impact[counter|length].form.hidden_tag() }} both don't work :( {{ form.impact[counter|length].hidden_tag() }}{{ form.impact[counter|length].form.hidden_tag() }}都不起作用:(

I'm getting: {'impact': [{'csrf_token': ['CSRF token missing']}, {'csrf_token': ['CSRF token missing']}]} 我得到: {'impact': [{'csrf_token': ['CSRF token missing']}, {'csrf_token': ['CSRF token missing']}]}

EDIT 18:22 编辑18:22

It seems that this: Form validation fails due missing CSRF is the solution. 看起来这样: 表单验证失败,因为缺少CSRF是解决方案。 Investigating... 调查...

This: Form validation fails due missing CSRF is the solution. 这: 表单验证失败,因为缺少CSRF是解决方案。

In previous versions this wasn't needed, and after an installation of a new extension pip updated flask-wtf as well... 在以前的版本中,这不是必需的,并且在安装新的扩展点之后更新了flask-wtf ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM