Jun-11-2024, 05:19 PM
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.
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.')