Python Forum
using split in my flask wtf form
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using split in my flask wtf form
#1
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
Reply
#2
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.')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Running powershell command in flask wtf form robertkwild 3 125 1 hour ago
Last Post: robertkwild
  making a form purely with flask-wtf robertkwild 15 787 Jun-01-2024, 11:53 AM
Last Post: robertkwild
  error while inserting values into a table from form in flask in postgreSQL sahilsiddharth 3 7,339 Jun-05-2017, 07:49 PM
Last Post: sahilsiddharth

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020