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
#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


Messages In This Thread
using split in my flask wtf form - by robertkwild - Jun-11-2024, 04:05 PM
RE: using split in my flask wtf form - by deanhystad - Jun-11-2024, 05:19 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Beta6 of my password flask-wtf web form robertkwild 3 1,355 Jul-02-2024, 10:30 AM
Last Post: robertkwild
  Show powershell errors in flask-wtf web form robertkwild 14 3,068 Jun-30-2024, 03:15 PM
Last Post: robertkwild
  Running powershell command in flask wtf form robertkwild 10 2,770 Jun-27-2024, 09:49 AM
Last Post: robertkwild
  making a form purely with flask-wtf robertkwild 15 3,770 Jun-01-2024, 11:53 AM
Last Post: robertkwild
  error while inserting values into a table from form in flask in postgreSQL sahilsiddharth 3 8,571 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