librelist archives

« back to archive

WTForms SelectField Default Option

WTForms SelectField Default Option

From:
Alasdair Macmillan
Date:
2010-10-22 @ 17:01
Hi

Another beginner question:

I have a wtforms SelectField 

so

1) I want it in validation terms to be 'required' so if an option hasn't 
been selected it returns an error and
2) have a default option of:

	<OPTION SELECTED VALUE="">Select...</OPTION>

that will throw the error if a different option hasn't been chosen

I have so far:

class ProjectsForm(Form):    
    selecta = SelectField('Select User', [validators.Required()])

and

def admin():
	form.selecta.choices = [(key, combined[key]) for key in combined]

and in my template:

{{ form.selecta }}
{% if form.selecta.errors %}
	 <ul class="errors">{% for error in form.selecta.errors %}<li>{{ error 
}}</li>{% endfor %}</ul>
{% endif %}


I have the <select> options being filled with the correct keys & values 
but I just don't know how to make it required, return an error message AND
have a default value

Help much appreciated

AL

Re: [flask] WTForms SelectField Default Option

From:
danjac354@gmail.com
Date:
2010-10-22 @ 17:05
 form.selecta.choices = [(key, combined[key]) for key in combined]
 form.selecta.choices.insert(0, ("", "Select..."))

On 22 October 2010 18:01, Alasdair Macmillan <al@atomised.coop> wrote:
> Hi
>
> Another beginner question:
>
> I have a wtforms SelectField
>
> so
>
> 1) I want it in validation terms to be 'required' so if an option hasn't
been selected it returns an error and
> 2) have a default option of:
>
>        <OPTION SELECTED VALUE="">Select...</OPTION>
>
> that will throw the error if a different option hasn't been chosen
>
> I have so far:
>
> class ProjectsForm(Form):
>    selecta = SelectField('Select User', [validators.Required()])
>
> and
>
> def admin():
>        form.selecta.choices = [(key, combined[key]) for key in combined]
>
> and in my template:
>
> {{ form.selecta }}
> {% if form.selecta.errors %}
>         <ul class="errors">{% for error in form.selecta.errors %}<li>{{ 
error }}</li>{% endfor %}</ul>
> {% endif %}
>
>
> I have the <select> options being filled with the correct keys & values 
but I just don't know how to make it required, return an error message AND
have a default value
>
> Help much appreciated
>
> AL
>
>