Python Forum

Full Version: using split in my flask wtf form
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi all,

im trying to put a split command in my code like so

class PasswordForm(FlaskForm):
    un = StringField('Username', [InputRequired(message='please enter your Username')])
    name = un.split(".")
    op = PasswordField('Current Password', [InputRequired(message='please enter your current password')])
    np = PasswordField('New Password', [InputRequired(message='please enter your new password'), EqualTo('cnp', message='must match confirm new password'), Length(min=12), Regexp('.*[a-z]', message='must contain one lower case'), Regexp('.*[A-Z]', message='must contain one upper case'), Regexp('.*[0-9]', message='must contain one number'), Regexp('.*[\¬\!\"\£\$\%\^\&\*\(\)\_\+\`\-\=\{\}\:\@\~\<\>\?\[\]\;\'\#\,\.\/\\\|]', message='must contain one special character')])
    cnp = PasswordField('Confirm New Password')
but i get this error when i run it in cmd

C:\python>python password.py
C:\python\password.py:14: SyntaxWarning: invalid escape sequence '\!'
  np = PasswordField('New Password', [InputRequired(message='please enter your new password'), EqualTo('cnp', message='must match confirm new password'), Length(min=12), Regexp('.*[a-z]', message='must contain one lower case'), Regexp('.*[A-Z]', message='must contain one upper case'), Regexp('.*[0-9]', message='must contain one number'), Regexp('.*[\¬\!\"\£\$\%\^\&\*\(\)\_\+\`\-\=\{\}\:\@\~\<\>\?\[\]\;\'\#\,\.\/\\\|]', message='must contain one special character')])
Traceback (most recent call last):
  File "C:\python\password.py", line 10, in <module>
    class PasswordForm(FlaskForm):
  File "C:\python\password.py", line 12, in PasswordForm
    name = un.split(".")
           ^^^^^^^^
AttributeError: 'UnboundField' object has no attribute 'split'
any help will be greatly appreciated

thanks,
rob
un is a field, not a string. un.data would get the string, but I don't think un.data is available inside the form like that.

I think what you want to do is write a custom validator.

https://wtforms.readthedocs.io/en/2.3.x/validators/
https://stackoverflow.com/questions/5032...sing-flask

It would look something like the code below. Beware, untested.
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, ValidationError
from wtforms.validators import InputRequired
import re


class PasswordForm(FlaskForm):
    username = StringField('Username', [InputRequired(message='please enter your Username')])
    old_password = PasswordField('Current Password', [InputRequired(message='please enter your current password')])
    password = PasswordField('New Password', [InputRequired(message='please enter your new password')])
    confirm_password = PasswordField('Confirm New Password')

    def validate_password(form, field):
        """Validate password field."""
        if field.data == form.old_password.data:
            raise ValidationError('new password cannot be the same as current password.')
        if field.data != form.confirm_password.data:
            raise ValidationError('passwords do not match.')
        if len(field.data < 12):
            raise ValidationError('must be at least 12 characters long')
        if re.match(r"[A-Z]", field.data) is None:
            raise ValidationError('must contain an upper case letter.')
        if re.match(r"[a-z]", field.data) is None:
            raise ValidationError('must contain a lower case letter.')
        if re.match(r"[0-9]", field.data) is None:
            raise ValidationError('must contain a digit.')
        if re.match(r"[\¬\!\"\£\$\%\^\&\*\(\)\_\+\`\-\=\{\}\:\@\~\<\>\?\[\]\;\'\#\,\.\/\\\|]", field.data) is None:
            raise ValidationError('must contain a special character.')
        if form.username.data is not None:
            if field.data in form.username.data.split():
                raise ValidationError('cannot be your first or last name.')