Skip to content

Commit 110c25a

Browse files
Added note on client-side form validation
1 parent f3ce82d commit 110c25a

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

docs/customization.qmd

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ async def welcome(request: Request):
111111

112112
In this example, the `welcome.html` template will receive two pieces of context: the user's `request`, which is always passed automatically by FastAPI, and a `username` variable, which we specify as "Alice". We can then use the `{{ username }}` syntax in the `welcome.html` template (or any of its parent or child templates) to insert the value into the HTML.
113113

114+
#### Form validation strategy
115+
116+
While this template includes comprehensive server-side validation through Pydantic models and custom validators, it's important to note that server-side validation should be treated as a fallback security measure. If users ever see the `validation_error.html` template, it indicates that our client-side validation has failed to catch invalid input before it reaches the server.
117+
118+
Best practices dictate implementing thorough client-side validation via JavaScript and/or HTML `input` element `pattern` attributes to:
119+
- Provide immediate feedback to users
120+
- Reduce server load
121+
- Improve user experience by avoiding round-trips to the server
122+
- Prevent malformed data from ever reaching the backend
123+
124+
Server-side validation remains essential as a security measure against malicious requests that bypass client-side validation, but it should rarely be encountered during normal user interaction. See `templates/authentication/register.html` for a client-side form validation example.
125+
114126
### Writing type annotated code
115127

116128
Pydantic is used for data validation and serialization. It ensures that the data received in requests meets the expected format and constraints. Pydantic models are used to define the structure of request and response data, making it easy to validate and parse JSON payloads.

0 commit comments

Comments
 (0)