Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WTForms Can I add a placeholder attribute when I init a field?

Tags:

python

wtforms

I want to add a placeholder attribute on to the field in WTForms. How can I do it?

abc = TextField('abc', validators=[Required(), Length(min=3, max=30)], placeholder="test") 

The above code is not valid

How can I add a placeholder attribute with value?

like image 582
TheOneTeam Avatar asked Mar 17 '12 11:03

TheOneTeam


People also ask

How do I add placeholder text to my form?

First, create a new form or edit an existing one. Then, add a new field to your form or click on one in the form builder to open its field options. From here, you’ll need to click on the Advanced section to open it. This will display extra settings, including a Placeholder Text field. Simply enter the text you’d like to display, and you’re all set!

Is it possible to use placeholder in the constructor of wtforms?

placeholder is not supported in the Python constructor in WTforms 2.0.x and below. u are right, placeholder should be style and should not add in the structural elements. A placeholder is the same thing as a label. It is, therefore, content and not style. Setting it in a template makes it impossible to have generic templates.

How do I set up placeholder text for a dropdown field?

To set up placeholder text for a Dropdown field, you’ll first need to add one to your form. Then, click on the Dropdown field to open its field options. Here, you’ll need to click the Advanced tab and add your Placeholder Text. When you’re happy with your placeholder text, be sure to save your changes before exiting the form builder. That’s it!

Why should I use placeholder text?

Below, we’ll cover the most popular reasons for using placeholder text. In most cases, placeholder text is used to show example input to users. Even when the input might seem obvious to you, this is one way to make the process of filling out your form even easier for users.


Video Answer


2 Answers

Updated for WTForms 2.1

You can now as of WTForms 2.1 (December 2015) set rendering keywords by using the render_kw= parameter to the field constructor.

So the field would look like:

abc = StringField('abc', [InputRequired()], render_kw={"placeholder": "test"}) 

Note while this is possible; it does start to bridge the line between code and presentation; so use it wisely!


(Old answer, still true for versions older than WTForms 2.1)

placeholder is not supported in the Python constructor in WTforms 2.0.x and below.

However, you can do this easily in your template:

{{ form.abc(placeholder="test") }} 
like image 76
Crast Avatar answered Sep 20 '22 04:09

Crast


Correct answer is as follows:

abc = TextField('abc', validators=[Required(), Length(min=3, max=30)], description="test") 

As one can read in documenatation:

description – A description for the field, typically used for help text. 

Then in your template:

{% import 'forms.html' as forms %}  {% for field in form %}     {{ forms.render_field(field) }} {% endfor %} 

Where render_field is a macro that is defined in forms.html:

{% macro render_field(field) -%}  {% if field.type == 'CSRFTokenField' %}     {{ field }}      {% if field.errors %}         <div class="warning">You have submitted an invalid CSRF token</div>     {% endif %} {% elif field.type == 'HiddenField' %}     {{ field }} {# any other special case you may need #} {% else %}     <div class="form-group">         <label for="{{ field.label.field_id }}" class="col-sm-2 control-label">{{ field.label.text }}</label>         <div class="col-sm-10">             {{ field(placeholder=field.description) }}             {% if field.errors %}                 <div class="alert alert-danger" role="alert">                 {% for err in field.errors %}                     <p>{{ err|e }}</p>                 {% endfor %}                 </div>             {% endif %}         </div>     </div> {% endif %}  {%- endmacro %} 
like image 22
Drachenfels Avatar answered Sep 23 '22 04:09

Drachenfels