Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
- fix tests
- add warning/explaination
  • Loading branch information
devkral committed Jan 24, 2025
1 parent 1453ebc commit e353803
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
7 changes: 7 additions & 0 deletions docs/en/docs/extras/forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ dataclasses.
{!> ../../../docs_src/extras/form/model.py !}
```

## Using form as non-data, non-payload field

When using Form as a non-data, non-payload field, the form receives only the data which matches the field-name.

If you use form-data requests make sure the field content is valid json.
When you use json requests only the portion which matches the field-name is passed through the model wrapped by the form.

## Notes

As you could see from the examples, it is very simple and direct to use the `Form` in Esmerald and
Expand Down
3 changes: 2 additions & 1 deletion tests/forms/test_forms_route.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from typing import Optional, Union

import pytest
Expand Down Expand Up @@ -29,7 +30,7 @@ async def start(request: Request, form: Union[Model, None] = Form()) -> bytes:
response = client.get("/")
assert response.status_code == 200

response = client.post("/", data={"id": "733"})
response = client.post("/", data={"form": json.dumps({"id": "733"})})
assert response.status_code == 200


Expand Down
3 changes: 2 additions & 1 deletion tests/test_direct_responses.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from typing import Optional

from pydantic import BaseModel
Expand Down Expand Up @@ -64,7 +65,7 @@ async def start(request: Request, form: Optional[Model] = Form()) -> Template:
response = client.get("/", follow_redirects=False)
assert response.status_code == 301

response = client.post("/", data={"id": 55}, follow_redirects=False)
response = client.post("/", data={"form": json.dumps({"id": 55})}, follow_redirects=False)
assert response.status_code == 301


Expand Down

0 comments on commit e353803

Please sign in to comment.