Skip to content

Commit

Permalink
Try to integrate new email validation into fields
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffersonBledsoe committed Feb 15, 2024
1 parent 98276f5 commit efeb8b8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
from plone.schema.email import _isemail
from zExceptions import BadRequest
from zope.i18n import translate

from collective.volto.formsupport import _


class Field:
def __init__(self, field_data):
def _attribute(attribute_name):
Expand Down Expand Up @@ -43,6 +50,9 @@ def field_id(self):
def send_in_email(self):
return True

def validate(self, request):
return


class YesNoField(Field):
@property
Expand All @@ -61,11 +71,30 @@ def send_in_email(self):
return False


class EmailField(Field):
def validate(self, request):
if _isemail(self._value) is None:
raise BadRequest(
translate(
_(
"wrong_email",
default='Email not valid in "${field}" field.',
mapping={
"field": self.label,
},
),
context=request,
)
)


def construct_field(field_data):
if field_data.get("widget") == "single_choice":
return YesNoField(field_data)
elif field_data.get("field_type") == "attachment":
return AttachmentField(field_data)
elif field_data.get("field_type") in ["from", "email"]:
return EmailField(field_data)

return Field(field_data)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from plone.registry.interfaces import IRegistry
from plone.restapi.deserializer import json_body
from plone.restapi.services import Service
from plone.schema.email import _isemail
from Products.CMFPlone.interfaces.controlpanel import IMailSchema
from zExceptions import BadRequest
from zope.component import getMultiAdapter, getUtility
Expand Down Expand Up @@ -89,6 +88,8 @@ def reply(self):
}
)
self.fields = construct_fields(fields_data)
for field in self.fields:
field.validate(request=self.request)

if send_action:
try:
Expand Down Expand Up @@ -167,31 +168,6 @@ def validate_form(self):
name=self.block["captcha"],
).verify(self.form_data.get("captcha"))

self.validate_email_fields()

def validate_email_fields(self):
email_fields = [
x.get("field_id", "")
for x in self.block.get("subblocks", [])
if x.get("field_type", "") == "from"
]
for form_field in self.form_data.get("data", []):
if form_field.get("field_id", "") not in email_fields:
continue
if _isemail(form_field.get("value", "")) is None:
raise BadRequest(
translate(
_(
"wrong_email",
default='Email not valid in "${field}" field.',
mapping={
"field": form_field.get("label", ""),
},
),
context=self.request,
)
)

def validate_attachments(self):
attachments_limit = os.environ.get("FORM_ATTACHMENTS_LIMIT", "")
if not attachments_limit:
Expand Down

0 comments on commit efeb8b8

Please sign in to comment.