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

Can't use FormField due to required request in __init__ #8

Open
nickleman opened this issue Jul 4, 2021 · 3 comments
Open

Can't use FormField due to required request in __init__ #8

nickleman opened this issue Jul 4, 2021 · 3 comments

Comments

@nickleman
Copy link

nickleman commented Jul 4, 2021

StarletteForm classes can't be used in FormField. This is becuase the initializer requires a Request operator. The request should be optional in the initializer and maybe pasted in with the validate_on_submit. Possible there needs to be a StarletteFormField class that passes its request object to the form passed in to it? But I think the best route would be to have an init that doesn't require positional arguments just like WTForms' Form class and then just use a load_form_data(request) member function to populate data. To be backwards compatible, probably keeping the request object as optional in the init function would be smart.

@amorey
Copy link
Member

amorey commented Jul 5, 2021

Thanks for creating an issue. Can you write an example code block that shows the problem you're describing?

@nickleman
Copy link
Author

class AddressForm(StarletteForm):
    street_addr = TextField(
        "Street Address",
        validators=[],
    )
    city = TextField(
        "City",
        validators=[],
    )
    state = TextField("State", validators=[Length(min=2, max=2)])
    zipcode = TextField("Zipcode", validators=[Length(min=5, max=5)])

class CreateSchoolForm(StarletteForm):
    name = TextField(
        "School Name",
        validators=[DataRequired("Please provide a school name")],
    )
    address = FormField(AddressForm)

Do you need the full working code? I'm extracting this out of something I'm writing.

@amorey
Copy link
Member

amorey commented Jul 5, 2021

Thanks! That helps.

I have to think some more about the consequences of allowing StarletteForm to be initialized without the request object (e.g. on CSRF protection) but in the mean time you should be able to use the WTF Form class for your inner forms:

from starlette_wtf import StarletteForm
from wtforms import Form, TextField, FormField

class AddressForm(Form):
    street_addr = TextField(
        "Street Address",
        validators=[],
    )
    city = TextField(
        "City",
        validators=[],
    )
    state = TextField("State", validators=[Length(min=2, max=2)])
    zipcode = TextField("Zipcode", validators=[Length(min=5, max=5)])

class CreateSchoolForm(StarletteForm):
    name = TextField(
        "School Name",
        validators=[DataRequired("Please provide a school name")],
    )
    address = FormField(AddressForm)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants