简体   繁体   English

Flask + WTForms + SelectMultipleField和Dynamic Choices

[英]Flask + WTForms + SelectMultipleField and Dynamic Choices

I am trying to use WTForms.SelectMultipleField to manage some dynamic choices on a form but I'm running into some difficulty with it being modified client-side before being submitted for validation. 我正在尝试使用WTForms.SelectMultipleField来管理表单上的一些动态选择但是我遇到了一些困难,它在被提交验证之前被修改为客户端。

Basically I have two SelectMultipleField options: 基本上我有两个SelectMultipleField选项:

class MyForm(Form):
    assigned = SelectMultipleField('Assigned', choices=[])
    available = SelectMultipleField('Available', choices=[('1','1'),('2','2')])

I'm using Flask to render Jinja2 templates like so: 我正在使用Flask渲染Jinja2模板,如下所示:

@app.view("/myview", methods=['GET','POST'])
def myview():
    form = MyForm(request.form)
    if request.method == 'POST' and form.validate():
        return render_template("success.html")
    else:
        return render_template("index.html", form=form)

In my template I have this: 在我的模板中,我有这个:

<script type="text/javascript">
    function assign_object() {
        return !$('#available option:selected').remove().appendTo('#assigned');
    };

    function unassign_object() {
        return !$('#assigned option:selected').remove().appendTo('#available');
    }

    $(document).ready( function() {
        $('#available').dblclick( assign_object );
        $('#assigned').dblclick( unassign_object );
    });
</script>

<form action="/myview" method="post" name="assign_objects">
    {{ render_field(form.a) }}
    {{ render_field(form.b) }}
    <input type="submit" value="Assign" name="assign_button"/>
</form>

Basically all of this works as intended; 基本上所有这些都按预期工作; double-clicking on an item in the unassigned list moves it over to the assigned list. 双击未分配列表中的项目会将其移动到指定的列表。 The issue is when the form is submitted for validation, because the .choices attribute on the "assigned" field was originally "[ ]" and is still expected to be "[ ]" rather than the new options list that we've given it. 问题是提交表单进行验证时,因为“已分配”字段上的.choices属性最初为“[]”,但仍然是“[]”而不是我们给出的新选项列表。

Does anyone know a good way to do this? 有谁知道这样做的好方法? I was thinking I could override the forms pre_validate() function and update assigned.choices to include all the values from the "available" list, but this doesn't feel "right" and could be used to submit random values from the client-side on submit. 我想我可以覆盖表单pre_validate()函数并更新assigned.choices以包含“可用”列表中的所有值,但这感觉不对“并且可以用来从客户端提交随机值 - 提交方面。

Cheers, David. 干杯,大卫。

Update choices in the POST request: 更新POST请求中的choices

AVAILABLE_CHOICES = [('1','1'),('2','2')]
DEFAULT_CHOICES = []

class MyForm(Form):
    assigned = SelectMultipleField('Assigned', choices=DEFAULT_CHOICES)
    available = SelectMultipleField('Available', choices=AVAILABLE_CHOICES)

@app.view("/myview", methods=['GET','POST'])
def myview():
    form = MyForm(request.form)
    if request.method == 'POST':
        form.assigned.choices = AVAILABLE_CHOICES
        if form.validate():
            return render_template("success.html")
        else:
            form.assigned.choices = DEFAULT_CHOICES

    return render_template("index.html", form=form)

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

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