Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validators in forms should pull from constants in their associated models #578

Open
brassy-endomorph opened this issue Sep 11, 2024 · 0 comments

Comments

@brassy-endomorph
Copy link
Collaborator

Is your feature request related to a problem? Please describe.

If there is a field that has a min or max size in a database model, there will be HTML forms that modify this field. A constant in the model should be used for both the SQLAlchemy column and the WTForm's validator.

For example, this display name field already has gotten out of sync (field size of 80 in the model, 100 in the form).

class User(Model):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
primary_username: Mapped[str] = mapped_column(db.String(80), unique=True)
display_name: Mapped[Optional[str]] = mapped_column(db.String(80))

class DisplayNameForm(FlaskForm):
display_name = StringField("Display Name", validators=[Length(max=100)])

Describe the solution you'd like

class MyModel(Model):
    MY_FIELD_MIN = 10
    MY_FIELD_MAX = 20
    ...
    my_field: Mapped[str] = mapped_column(db.String(MY_FIELD_MAX))

class MyModelForm(FlaskForm):
    my_field = StringField("My Field", validators=[Length(MyModel.MY_FIELD_MIN, MyModel.MY_FIELD_MAX)])
@brassy-endomorph brassy-endomorph changed the title Validators in forms should pull from contants in their associated models Validators in forms should pull from constants in their associated models Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: BL-P4 - DevOps & Code Tidiness
Development

No branches or pull requests

1 participant