Why am i getting this error: TypeError: LoginForm.validate() got an unexpected keyword argument 'extra_validators'

567 Views Asked by At

I am get ting t his error when i try to log in my application with flask, i am using mongodb for my database. This is all the code from app.py

from flask import Flask, render_template, request, redirect, url_for
from flask_mongoengine import MongoEngine
from flask_security import Security, MongoEngineUserDatastore, UserMixin, RoleMixin, login_required, login_user, logout_user, current_user, LoginForm

app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = 'super-secret-key'
app.config['SECURITY_PASSWORD_SALT'] = 'super-secret-salt'
app.config['MONGODB_SETTINGS'] = {
    'db': 'flask-login',
    'host': 'localhost',
    'port': 27017,
}

db = MongoEngine(app)

class Role(db.Document, RoleMixin):
    name = db.StringField(max_length=80)
    description = db.StringField(max_length=255)

class User(db.Document, UserMixin):
    name = db.StringField(max_length=255)
    email = db.StringField(max_length=255, unique=True)
    password = db.StringField(max_length=255)
    active = db.BooleanField(default=True)
    confirmed_at = db.DateTimeField()
    roles = db.ListField(db.ReferenceField(Role), default=[])

user_datastore = MongoEngineUserDatastore(db, User, Role)
security = Security(app, user_datastore)


@app.route('/dashboard')
@login_required
def dashboard():
    return render_template('dashboard.html')

@app.route('/')
def home():
    return render_template('home.html')

@app.route('/register', methods=['POST'])
def add_user():
    name = request.form['name']
    email = request.form['email']
    password = request.form['password']
    user = User(name=name, email=email, password=password)
    user.save()
    return redirect('/')

@app.route('/logout')
@login_required
def logout():
    logout_user()
    return redirect('/')

I tried to do a diferent login route but no success. The only "solution" to my problem was deleting "extra_validators=None" from the "validate_on_submit" function on the form.py file from flask_wtf package.

From this:

def validate_on_submit(self, extra_validators=None):
        """Call :meth:`validate` only if the form is submitted.
        This is a shortcut for ``form.is_submitted() and form.validate()``.
        """
        return self.is_submitted() and self.validate(extra_validators=extra_validators)

To this:

def validate_on_submit(self):
        """Call :meth:`validate` only if the form is submitted.
        This is a shortcut for ``form.is_submitted() and form.validate()``.
        """
        return self.is_submitted() and self.validate()

Then the login worked but i don't know if is right to modificate the package file

1

There are 1 best solutions below

4
jwag On

Make sure WTForms, Flask-WTForms, and Flask-Security-Too are all at the latest version. It should all work. If you still have issues - please post precisely what versions of these packages you are using.