-1

On a website I have a <select> element with multiple attribute like so:

<select value="name" multiple>
  <option value="willi">Willi</option>
  <option value="wonka">Wonka</option>
</select>

If both options are selected, then the Pyramid server’s view function receives the request object with a multidict instance:

 MultiDict([('name', 'willi'), ('name', 'wonka')])

Because I’ve been using Colander for data validation and deserialization, I wanted to write a schema for this instance… but after digging around the documentation I’m beginning to wonder if that’s at all possible?

So, how would I deserialize this particular multidict using Colander?

Jens
  • 8,423
  • 9
  • 58
  • 78
  • You can see example selects and their Colander schema in the [Deform demo](https://deformdemo.pylonsproject.org/select/). BTW, it would be a good idea to have an empty string value, else the select will submit the first option if the user does not deliberately choose an option. – Steve Piercy Sep 30 '20 at 03:28
  • Thanks @StevePiercy! Found a [demo with select multiple](https://deformdemo.pylonsproject.org/select_with_multiple/) on that page: I see it uses a generic [colander.Schema](https://colander.readthedocs.io/en/latest/api.html#colander.Schema) with a [Set](https://colander.readthedocs.io/en/latest/api.html#colander.Set) type node. The `widget` property is new to me, and I’m not using [deform](https://github.com/Pylons/deform) in my project… Hm. Perhaps a manual implementation of [deserialize()](https://colander.readthedocs.io/en/latest/api.html#colander.SchemaNode.deserialize) would work? – Jens Sep 30 '20 at 03:46
  • I have not used Colander without Deform or Peppercorn, but one could use Colander without them, as you have found in Colander's API docs. Here's an [example in the Colander narrative docs](https://docs.pylonsproject.org/projects/colander/en/latest/basics.html#deserialization). – Steve Piercy Sep 30 '20 at 03:57

1 Answers1

0

I suspect you'll want to use something like request.POST.mixed() which will return a list for the name key if multiple values were sent (but not a list if just one value) combined with maybe a colander preparer to convert it to a list if it isn't already.

Michael Merickel
  • 23,153
  • 3
  • 54
  • 70