diff --git a/project/jinja2env.py b/project/jinja2env.py
index f7c893f..ba260fb 100644
--- a/project/jinja2env.py
+++ b/project/jinja2env.py
@@ -1,10 +1,20 @@
+from typing import Any
+
from jinja2 import Environment
+def conditional_cls(conditions: dict[str, Any]) -> str:
+ result = []
+ for cls, condition in conditions.items():
+ if condition:
+ result.append(cls)
+ return " ".join(result)
+
+
def environment(**options):
env = Environment(**options)
env.globals.update({
- # "url": reverse_url,
+ "conditional_cls": conditional_cls,
})
return env
diff --git a/project/main/templates/main/form_validation.html b/project/main/templates/main/form_validation.html
index dc554bf..f6d8c83 100644
--- a/project/main/templates/main/form_validation.html
+++ b/project/main/templates/main/form_validation.html
@@ -2,7 +2,18 @@
{% block content %}
-
- This is some form validation content bellow.
-
+
{% endblock %}
diff --git a/project/main/templates/main/form_validation_content.html b/project/main/templates/main/form_validation_content.html
new file mode 100644
index 0000000..e4ff437
--- /dev/null
+++ b/project/main/templates/main/form_validation_content.html
@@ -0,0 +1,59 @@
+
+
+
+
+
+
+ {% if validation.name_error %}
+
{{ validation.name_error }}
+ {% endif %}
+
+
+
+
+
+
+ {% if validation.age_error %}
+
{{ validation.age_error }}
+ {% endif %}
+
+
+
+
+
+
diff --git a/project/main/views/form_validation.py b/project/main/views/form_validation.py
index 6d64900..a462d72 100644
--- a/project/main/views/form_validation.py
+++ b/project/main/views/form_validation.py
@@ -1,7 +1,66 @@
+from dataclasses import dataclass
+from typing import Any, Optional
+
+from django.core.handlers.wsgi import WSGIRequest
+from django.http import HttpResponse
+from django.shortcuts import render
+
from project.main.views.demo_view_base import DemoViewBase
+@dataclass
+class Validation:
+ validated: bool = False
+
+ name: Optional[str] = None
+ consent: Optional[bool] = None
+ age: Optional[int] = None
+
+ name_error: Optional[str] = None
+ age_error: Optional[str] = None
+ consent_error: Optional[str] = None
+
+
class FormValidationView(DemoViewBase):
template_name = "main/form_validation.html"
active_section = "form-validation"
title = "Form Validation"
+
+ def get_context_data(self, **kwargs) -> dict[str, Any]:
+ context = super().get_context_data(**kwargs)
+ context.update(
+ {
+ "validation": Validation(),
+ }
+ )
+ return context
+
+ def post(self, request: WSGIRequest, *args, **kwargs) -> HttpResponse:
+ validation = Validation(
+ validated=True,
+ name=request.POST.get("name", ""),
+ age=request.POST.get("age", ""),
+ consent=request.POST.get("consent") == "on",
+ )
+
+ if len(validation.name) < 3 or len(validation.name) > 10:
+ validation.name_error = "Name must be between 3 and 10 chars long"
+
+ try:
+ age = int(validation.age)
+ except ValueError:
+ validation.age_error = "Invalid integer"
+ else:
+ if age < 1 or age > 100:
+ validation.age_error = "Age should be between 1 and 100"
+
+ if not validation.consent:
+ validation.consent_error = "You should consent"
+
+ return render(
+ context={
+ "validation": validation,
+ },
+ template_name="main/form_validation_content.html",
+ request=request,
+ )