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

Using async_validate_{field} with Form's prefix #15

Closed
zyzniewski opened this issue Jan 24, 2024 · 9 comments · Fixed by #30 or #31
Closed

Using async_validate_{field} with Form's prefix #15

zyzniewski opened this issue Jan 24, 2024 · 9 comments · Fixed by #30 or #31

Comments

@zyzniewski
Copy link

class MyForm(starlette_wtf.StarletteForm):
    public = wtforms.BooleanField()

    def validate_public(self, field):
        print("YEP")

    async def async_validate_public(self, field):
        print("NOPE")

form = await MyForm.from_formdata(
    request,
    prefix="registers",
)
await form.validate()

raises the following exception:

  File "/home/zyzniewski/Projects/ChessManager/ChessManagerWeb/.venv/lib/python3.11/site-packages/starlette_wtf/form.py", line 156, in validate
    tasks = [self._validate_async(*async_validators[name]) for name in \
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/zyzniewski/Projects/ChessManager/ChessManagerWeb/.venv/lib/python3.11/site-packages/starlette_wtf/form.py", line 156, in <listcomp>
    tasks = [self._validate_async(*async_validators[name]) for name in \
                                   ~~~~~~~~~~~~~~~~^^^^^^
KeyError: 'registers-public'

caused by using field.name that contains Form's prefix: https://github.com/muicss/starlette-wtf/blob/f96ab6f33a7ffcb576663f996dbb401ab44f3d78/starlette_wtf/form.py#L143

amorey added a commit to amorey/starlette-wtf that referenced this issue Apr 9, 2024
amorey added a commit that referenced this issue Apr 9, 2024
@amorey amorey mentioned this issue Apr 9, 2024
amorey added a commit that referenced this issue Apr 9, 2024
* Added support for form prefixes (#15)
* Fixed type hint issue in CSRFProtectMiddleware (#17)
* Fixed deprecation warnings in tests
* Updated README to latest dev best practices
* Fixed bugs in examples, added READMEs and requirements.txt files

---------

Co-authored-by: Le Tuan Hai <letuanhai@live.com>
@amorey
Copy link
Member

amorey commented Apr 9, 2024

Sorry, I missed this earlier! This is fixed in the latest version (0.4.4). Thanks the bug report!

@amorey amorey closed this as completed Apr 9, 2024
@zyzniewski
Copy link
Author

zyzniewski commented Apr 9, 2024

Hi, just got an exception:

KeyError: 'registers-registers-public'

at .<listcomp> ( /layers/google.python.pip/pip/lib/python3.11/site-packages/starlette_wtf/form.py:155 )
at .validate ( /layers/google.python.pip/pip/lib/python3.11/site-packages/starlette_wtf/form.py:155 )`

@amorey
Copy link
Member

amorey commented Apr 9, 2024

How is prefix being invoked? Can you share the code triggering the error?

@zyzniewski
Copy link
Author

form = await Step2regs.from_formdata(
    request,
    meta={
        "tournament": tournament,
        "user": admin,
    },
    prefix="registers",
)

if await form.validate():
    ...

@amorey
Copy link
Member

amorey commented Apr 9, 2024

Thanks, can you share the form class definition and the template as well? There’s a unit test that has a similar setup to your code snippet so just based on this snippet I’m not sure where the second prefix is coming from.

@amorey amorey reopened this Apr 9, 2024
@zyzniewski
Copy link
Author

zyzniewski commented Apr 9, 2024

amorey added a commit to amorey/starlette-wtf that referenced this issue Apr 9, 2024
amorey added a commit that referenced this issue Apr 9, 2024
@amorey
Copy link
Member

amorey commented Apr 9, 2024

Got it, thanks. Try v0.4.5rc1 and let me know if that fixes the problem:

pip install starlette-wtf==0.4.5rc1

@zyzniewski
Copy link
Author

That seems to work, thanks!

@amorey amorey mentioned this issue Apr 10, 2024
amorey added a commit that referenced this issue Apr 10, 2024
* Fixed #15
* Bumped version number
@amorey
Copy link
Member

amorey commented Apr 10, 2024

Awesome! Thanks again for the suggestion! I bumped the version number (0.4.5) and published it to pypi. Let me know if you run into any more problems.

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

Successfully merging a pull request may close this issue.

2 participants