Dynamic-flag box-wtf with empty option

I want to fill in a selection box based on a search query.

But I also want an empty option.

This is my current code.

 form.state.choices=[(s.id, s.name) for s in State.query.all()]

result

  <select>
  <option value="CA">California</option>
  <option value="FL">Florida</option>
  </select>

desired result

<select>
<option value=""></option>
<option value="CA">California</option>
<option value="FL">Florida</option>
</select>

It would be great if also invalid if the selection option is empty.

+4
source share
1 answer

You might want to add an empty value as follows:

choices = [("", "---")]
form.state.choices=[choices.append((s.id, s.name)) for s in State.query.all()]

Then you can check if the value is an empty string to check the field.

+5
source

Source: https://habr.com/ru/post/1662501/


All Articles