Restrict user input to more sensible ranges #251
1 changed files with 18 additions and 3 deletions
|
|
@ -9,7 +9,17 @@ from servala.core.odoo import get_invoice_addresses, get_odoo_countries
|
||||||
from servala.frontend.forms.mixins import HtmxMixin
|
from servala.frontend.forms.mixins import HtmxMixin
|
||||||
|
|
||||||
|
|
||||||
|
ORG_NAME_PATTERN = r"[\w\s\-.,&'()+]+"
|
||||||
|
|
||||||
|
|
||||||
class OrganizationForm(HtmxMixin, ModelForm):
|
class OrganizationForm(HtmxMixin, ModelForm):
|
||||||
|
name_validator = RegexValidator(
|
||||||
|
regex=f"^{ORG_NAME_PATTERN}$",
|
||||||
|
message=_(
|
||||||
|
"Organization name can only contain letters, numbers, spaces, and common punctuation (-.,&'()+)."
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
# def __init__(self, *args, **kwargs):
|
# def __init__(self, *args, **kwargs):
|
||||||
# super().__init__(*args, **kwargs)
|
# super().__init__(*args, **kwargs)
|
||||||
# if self.instance and self.instance.has_inherited_billing_entity:
|
# if self.instance and self.instance.has_inherited_billing_entity:
|
||||||
|
|
@ -20,15 +30,20 @@ class OrganizationForm(HtmxMixin, ModelForm):
|
||||||
widgets = {
|
widgets = {
|
||||||
"name": forms.TextInput(
|
"name": forms.TextInput(
|
||||||
attrs={
|
attrs={
|
||||||
"maxlength": "32",
|
"maxlength": "100",
|
||||||
"pattern": "[A-Za-z0-9\\s]+",
|
"pattern": ORG_NAME_PATTERN,
|
||||||
"title": _(
|
"title": _(
|
||||||
"Organization name can only contain letters, numbers, and spaces"
|
"Organization name can contain letters, numbers, spaces, and common punctuation (-.,&'()+). Emoji not allowed."
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
self.fields["name"].validators.append(self.name_validator)
|
||||||
|
self.fields["name"].max_length = 100
|
||||||
|
|
||||||
|
|
||||||
class OrganizationCreateForm(OrganizationForm):
|
class OrganizationCreateForm(OrganizationForm):
|
||||||
address_validator = RegexValidator(
|
address_validator = RegexValidator(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue