Files
santeh-ray/home/forms.py
Azimkin aca8f56b9a
All checks were successful
Build and Push Docker Image / build (push) Successful in 52s
Small fixes
2026-06-24 16:19:52 +02:00

76 lines
2.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from django import forms
PHONE_MIN_DIGITS = 9
PHONE_MAX_DIGITS = 15
class FaqQuestionForm(forms.Form):
name = forms.CharField(
label="Имя",
max_length=150,
widget=forms.TextInput(
attrs={
"placeholder": "Имя",
"autocomplete": "name",
"title": "Имя не должно содержать цифры",
"data-faq-name-input": "",
}
),
)
phone = forms.CharField(
label="Номер телефона",
max_length=64,
widget=forms.TextInput(
attrs={
"placeholder": "Номер телефона",
"autocomplete": "tel",
"inputmode": "tel",
"pattern": r"^\+?\d+(?:-\d+)*$",
"title": "Телефон должен начинаться с + или цифры и содержать только цифры и дефисы",
"data-faq-phone-input": "",
}
),
)
question = forms.CharField(
label="Ваш вопрос",
max_length=1000,
widget=forms.Textarea(attrs={"placeholder": "Ваш вопрос", "rows": 4, "autocomplete": "off"}),
)
accept = forms.BooleanField(
label="Я согласен с политикой обработки персональных данных",
error_messages={"required": "Необходимо согласие на обработку персональных данных."},
)
def clean_name(self):
name = self.cleaned_data["name"].strip()
if any(char.isdigit() for char in name):
raise forms.ValidationError("Имя не должно содержать цифры.")
return name
def clean_phone(self):
phone = self.cleaned_data["phone"].strip()
digits_count = sum(char.isdigit() for char in phone)
if not phone or not (phone[0].isdigit() or phone[0] == "+"):
raise forms.ValidationError("Телефон должен начинаться с + или цифры.")
if phone.startswith("+"):
phone_body = phone[1:]
else:
phone_body = phone
if not phone_body or any(not (char.isdigit() or char == "-") for char in phone_body):
raise forms.ValidationError("Телефон может содержать только цифры и дефисы.")
if "--" in phone_body or phone_body.startswith("-") or phone_body.endswith("-"):
raise forms.ValidationError("Дефис в телефоне должен быть между цифрами.")
if not (PHONE_MIN_DIGITS <= digits_count <= PHONE_MAX_DIGITS):
raise forms.ValidationError("Телефон должен содержать от 9 до 15 цифр.")
return phone
def clean_question(self):
return self.cleaned_data["question"].strip()