Guim
Guim

Reputation: 678

Setting MIN LENGTH in WTFORM attributes

I have 3 inputs and a textarea in a Flask form. When those are valid I change the CSS with the following code:

input:valid ~ label,
textarea:valid ~ label,
input:onfocus ~ label,
textarea:onfocus ~ label
{
    /* Change style */ 
}

For the inputs there's no problem because they have the required attribute. But the textarea is optional. So all the time is valid and the CSS is applied immediately. I would like to set a minlengthto the textare so it's only valid when length < 1. I tried the following code in the wtformbut nothing happens.

{{ form.textareaName(minlength=1) }}
# I also tried with a String
{{ form.textareaName(minlength="1") }}

Some solution? Thanks!

Upvotes: 1

Views: 5358

Answers (1)

simanacci
simanacci

Reputation: 2344

Use Length validator it validates the length of a string. You can set the minimum and maximum length required. i.e.

from wtforms.validators import Length

In your forms.py,

textareaName = TextAreaField(validators=[Length(min=2)])

Upvotes: 4

Related Questions