Skip to content

Commit

Permalink
feat: validate email field
Browse files Browse the repository at this point in the history
  • Loading branch information
giuliaghisini committed Feb 27, 2024
1 parent 68ad554 commit 6572342
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/components/View.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import FormView from 'volto-form-block/components/FormView';
import { formatDate } from '@plone/volto/helpers/Utils/Date';
import config from '@plone/volto/registry';
import { Captcha } from 'volto-form-block/components/Widget';
import { isValidEmail } from 'volto-form-block/helpers/validators';

const messages = defineMessages({
formSubmitted: {
Expand Down Expand Up @@ -119,6 +120,7 @@ const View = ({ data, id, path }) => {
config.blocks.blocksConfig.form.additionalFields?.filter(
(f) => f.id === fieldType && f.isValid !== undefined,
)?.[0] ?? null;

if (
subblock.required &&
additionalField &&
Expand All @@ -138,6 +140,12 @@ const View = ({ data, id, path }) => {
JSON.stringify(formData[name]?.value ?? {}) === '{}')
) {
v.push(name);
} else if (
fieldType === 'from' &&
formData[name]?.value &&
!isValidEmail(formData[name].value)
) {
v.push(name);
}
});

Expand Down Expand Up @@ -169,9 +177,10 @@ const View = ({ data, id, path }) => {
let name = getFieldName(subblock.label, subblock.id);
if (formattedFormData[name]?.value) {
formattedFormData[name].field_id = subblock.field_id;
const isAttachment = config.blocks.blocksConfig.form.attachment_fields.includes(
subblock.field_type,
);
const isAttachment =
config.blocks.blocksConfig.form.attachment_fields.includes(
subblock.field_type,
);
const isDate = subblock.field_type === 'date';

if (isAttachment) {
Expand Down
8 changes: 8 additions & 0 deletions src/helpers/validators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const isValidEmail = (email) => {
// Email Regex taken from from WHATWG living standard:
// https://html.spec.whatwg.org/multipage/input.html#e-mail-state-(type=email)
const emailRegex =
/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)$/;
const isValid = emailRegex.test(email);
return isValid;
};

0 comments on commit 6572342

Please sign in to comment.