pallets-eco / flask-admin

Simple and extensible administrative interface framework for Flask
https://flask-admin.readthedocs.io
BSD 3-Clause "New" or "Revised" License
5.8k stars 1.58k forks source link

AttributeError: module 'wtforms.validators' has no attribute 'required' #2226

Open pmcknig1 opened 2 years ago

pmcknig1 commented 2 years ago

Running the auth-flask-admin example errors with the following `AttributeError: module 'wtforms.validators' has no attribute 'required'

This is directly after pip installing the requirements and running python examples/auth-flask-login/app.py

The console output is below.

(venv) phimckni:flask-admin$ python examples/auth-flask-login/app.py
/Users/phimckni/Repos/flask-admin/venv/lib/python3.9/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 "/Users/phimckni/Repos/flask-admin/examples/auth-flask-login/app.py", line 58, in <module>
    class LoginForm(form.Form):
  File "/Users/phimckni/Repos/flask-admin/examples/auth-flask-login/app.py", line 59, in LoginForm
    login = fields.StringField(validators=[validators.required()])
AttributeError: module 'wtforms.validators' has no attribute 'required'
(venv) phimckni:flask-admin$ python --version
Python 3.9.10
(venv) phimckni:flask-admin$

and pip freeze just to show to correct requirements.txt was used.

(venv) phimckni:flask-admin$ pip freeze
click==8.0.3
Flask==2.0.3
Flask-Admin==1.6.0
Flask-Login==0.5.0
Flask-SQLAlchemy==2.5.1
greenlet==1.1.2
itsdangerous==2.0.1
Jinja2==3.0.3
MarkupSafe==2.0.1
SQLAlchemy==1.4.31
Werkzeug==2.0.3
WTForms==3.0.1
pmcknig1 commented 2 years ago

looks like wtforms.validators split the Required into DataRequired and InputRequired in version version 1.0.2

Looks like changing lines 59, 60, 79, and 81 from

    login = fields.StringField(validators=[validators.required()])
    password = fields.PasswordField(validators=[validators.required()])

and

    login = fields.StringField(validators=[validators.required()])
    email = fields.StringField()
    password = fields.PasswordField(validators=[validators.required()])

to:

    login = fields.StringField(validators=[validators.InputRequired()])
    password = fields.PasswordField(validators=[validators.InputRequired()])
    login = fields.StringField(validators=[validators.InputRequired()])
    email = fields.StringField()
    password = fields.PasswordField(validators=[validators.InputRequired()])

fixes the issue.

I would also suggest when getting a working example again, running pip freeze > examples/auth-flask-login/requirements.txt so that specific module version are saved. That way the examples are future proofed for the next set of people to clone the repository.

dkarneman commented 1 year ago

It looks like this was fixed by https://github.com/flask-admin/flask-admin/pull/2308. Consider closing.