title: Flask + WTForms + SelectMultipleField and Dynamic Choices
body: <p>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.</p>
  
  <p>Basically I have two SelectMultipleField options:</p>
  
  <pre><code>class MyForm(Form):
      assigned = SelectMultipleField('Assigned', choices=[])
      available = SelectMultipleField('Available', choices=[('1','1'),('2','2')])
  </code></pre>
  
  <p>I'm using Flask to render Jinja2 templates like so:</p>
  
  <pre><code>@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)
  </code></pre>
  
  <p>In my template I have this:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      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 );
      });
  &lt;/script&gt;
  
  &lt;form action="/myview" method="post" name="assign_objects"&gt;
      {{ render_field(form.a) }}
      {{ render_field(form.b) }}
      &lt;input type="submit" value="Assign" name="assign_button"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Cheers,
  David.</p>