Search code examples
pythonflaskflask-wtforms

for validator in validators: TypeError: 'Length' object is not iterable


i am facing this error making the form validation

for validator in validators: TypeError: 'Length' object is not iterable

from flask_wtf import FlaskForm
from wtforms import StringField,PasswordField,SubmitField
from wtforms.validators import Length

class RegisterForm(FlaskForm):
       username=StringField(label='User Name',validators=Length(2,30))
       email_address=StringField(label='E-mail')
       password1=PasswordField(label='Password')
       password2=PasswordField(label='Confirm Password')
       submit=SubmitField(label='Create Account')

here is the complete error log

C:\Users\navee\AppData\Local\Programs\Python\Python310\lib\site-packages\flask_sqlalchemy_init_.py:872: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning. warnings.warn(FSADeprecationWarning( Traceback (most recent call last): File "C:\Users\navee\Desktop\sikander\Coding\Python\Flaskmarket\run.py", line 1, in from market import app File "C:\Users\navee\Desktop\sikander\Coding\Python\Flaskmarket\market_init_.py", line 9, in from market import routes File "C:\Users\navee\Desktop\sikander\Coding\Python\Flaskmarket\market\routes.py", line 4, in from market.forms import RegisterForm File "C:\Users\navee\Desktop\sikander\Coding\Python\Flaskmarket\market\forms.py", line 5, in class RegisterForm(FlaskForm): File "C:\Users\navee\Desktop\sikander\Coding\Python\Flaskmarket\market\forms.py", line 6, in RegisterForm username=StringField(label='User Name',validators=Length(2,30)) File "C:\Users\navee\AppData\Local\Programs\Python\Python310\lib\site-packages\wtforms\fields\core.py", line 33, in new return UnboundField(cls, *args, **kwargs) File "C:\Users\navee\AppData\Local\Programs\Python\Python310\lib\site-packages\wtforms\fields\core.py", line 384, in init self.field_class.check_validators(validators) File "C:\Users\navee\AppData\Local\Programs\Python\Python310\lib\site-packages\wtforms\fields\core.py", line 177, in check_validators for validator in validators: TypeError: 'Length' object is not iterable


Solution

  • WTForms expects validators to be a list (thus the plural), so it can iterate over it. Your code should be:

    username=StringField(label='User Name',validators=[Length(2,30)])