Search code examples
flaskflask-security

Flask-Security-Too 5.3: Add field to registration form


I have a Flask app using Flask-Security-Too and I want to add a custom field to my registration form. I've followed the docs at https://flask-security-too.readthedocs.io/en/stable/customizing.html but my custom field simply doesn't get added. I can overwrite the form entirely using my own form, but that's not what I desire.

These are the steps I've taken to add an aka field to my registration form:

class RegistrationForm(RegisterForm):
    aka = StringField('Nickname (Optional)', [
        Length(min=4, max=20),
        Regexp('^[A-Za-z][A-Za-z0-9_.]*$', 0,
               'Nicknames must have only letters, '
               'numbers, dots or underscores')
    ], render_kw={"placeholder": "Enter your preferred name"})
security = Security(datastore=self.user_datastore, register_form=RegistrationForm)
security.init_app(app)
class User(UserMixin, db.Model):
    id = db.Column(db.String(255),
                   primary_key=True,
                   default=lambda: str(uuid.uuid4()))
    fs_uniquifier = db.Column(db.String(255), 
                              unique=True, 
                              nullable=False)
    email = db.Column(db.String(254), 
                      nullable=False, 
                      unique=True)
    aka = db.Column(db.String(20), 
                    nullable=True)
    password = db.Column(db.String(128), 
                         nullable=False)

There is no field rendered or in page source

Vanilla Flask-Security-Too registration form


Solution

  • As the 'tip' on that page says:

    Changing/extending the form class won’t directly change how it is displayed. You need to ALSO provide your own template and explicitly add the new fields you want displayed.

    See if that helps!