Skip to content

Commit

Permalink
feat: added default validators for field default_from and default_to
Browse files Browse the repository at this point in the history
  • Loading branch information
giuliaghisini committed Mar 18, 2024
1 parent 61bf777 commit ba41002
Show file tree
Hide file tree
Showing 16 changed files with 184 additions and 4 deletions.
10 changes: 10 additions & 0 deletions locales/de/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,16 @@ msgstr "Daten exportieren"
msgid "form_edit_fill_required_configuration_fields"
msgstr ""

#: index
# defaultMessage: The e-mail entered in the 'From' field must be a valid e-mail address.
msgid "form_edit_invalid_from_email"
msgstr ""

#: index
# defaultMessage: The e-mail entered in the 'Receipients' field must be a valid e-mail address.
msgid "form_edit_invalid_to_email"
msgstr ""

#: components/ValidateConfigForm
# defaultMessage: Please, verify this configuration errors in sidebar. The form will be not displayed in view mode until this errors fields are not fixed.
msgid "form_edit_other_errors"
Expand Down
10 changes: 10 additions & 0 deletions locales/en/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,16 @@ msgstr "Export in CSV"
msgid "form_edit_fill_required_configuration_fields"
msgstr ""

#: index
# defaultMessage: The e-mail entered in the 'From' field must be a valid e-mail address.
msgid "form_edit_invalid_from_email"
msgstr ""

#: index
# defaultMessage: The e-mail entered in the 'Receipients' field must be a valid e-mail address.
msgid "form_edit_invalid_to_email"
msgstr ""

#: components/ValidateConfigForm
# defaultMessage: Please, verify this configuration errors in sidebar. The form will be not displayed in view mode until this errors fields are not fixed.
msgid "form_edit_other_errors"
Expand Down
10 changes: 10 additions & 0 deletions locales/es/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,16 @@ msgstr "Exportar datos"
msgid "form_edit_fill_required_configuration_fields"
msgstr ""

#: index
# defaultMessage: The e-mail entered in the 'From' field must be a valid e-mail address.
msgid "form_edit_invalid_from_email"
msgstr ""

#: index
# defaultMessage: The e-mail entered in the 'Receipients' field must be a valid e-mail address.
msgid "form_edit_invalid_to_email"
msgstr ""

#: components/ValidateConfigForm
# defaultMessage: Please, verify this configuration errors in sidebar. The form will be not displayed in view mode until this errors fields are not fixed.
msgid "form_edit_other_errors"
Expand Down
10 changes: 10 additions & 0 deletions locales/eu/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,16 @@ msgstr "Esportatu datuak"
msgid "form_edit_fill_required_configuration_fields"
msgstr ""

#: index
# defaultMessage: The e-mail entered in the 'From' field must be a valid e-mail address.
msgid "form_edit_invalid_from_email"
msgstr ""

#: index
# defaultMessage: The e-mail entered in the 'Receipients' field must be a valid e-mail address.
msgid "form_edit_invalid_to_email"
msgstr ""

#: components/ValidateConfigForm
# defaultMessage: Please, verify this configuration errors in sidebar. The form will be not displayed in view mode until this errors fields are not fixed.
msgid "form_edit_other_errors"
Expand Down
10 changes: 10 additions & 0 deletions locales/fr/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,16 @@ msgstr "Exporter au format CSV"
msgid "form_edit_fill_required_configuration_fields"
msgstr ""

#: index
# defaultMessage: The e-mail entered in the 'From' field must be a valid e-mail address.
msgid "form_edit_invalid_from_email"
msgstr ""

#: index
# defaultMessage: The e-mail entered in the 'Receipients' field must be a valid e-mail address.
msgid "form_edit_invalid_to_email"
msgstr ""

#: components/ValidateConfigForm
# defaultMessage: Please, verify this configuration errors in sidebar. The form will be not displayed in view mode until this errors fields are not fixed.
msgid "form_edit_other_errors"
Expand Down
10 changes: 10 additions & 0 deletions locales/it/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,16 @@ msgstr "Esporta in CSV"
msgid "form_edit_fill_required_configuration_fields"
msgstr "Inserire i campi obbligatori per la configurazione del form nella sidebar di destra. Il form non verrà mostrato sul sito finché i campi obbligatori non saranno configurati."

#: index
# defaultMessage: The e-mail entered in the 'From' field must be a valid e-mail address.
msgid "form_edit_invalid_from_email"
msgstr "L'e-mail inserita nel campo 'Mittente di default' deve essere un indirizzo e-mail valido"

#: index
# defaultMessage: The e-mail entered in the 'Receipients' field must be a valid e-mail address.
msgid "form_edit_invalid_to_email"
msgstr "Le e-mail inserite nel campo 'Destinatari' devono essere indirizzi e-mail validi."

#: components/ValidateConfigForm
# defaultMessage: Please, verify this configuration errors in sidebar. The form will be not displayed in view mode until this errors fields are not fixed.
msgid "form_edit_other_errors"
Expand Down
10 changes: 10 additions & 0 deletions locales/ja/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,16 @@ msgstr ""
msgid "form_edit_fill_required_configuration_fields"
msgstr ""

#: index
# defaultMessage: The e-mail entered in the 'From' field must be a valid e-mail address.
msgid "form_edit_invalid_from_email"
msgstr ""

#: index
# defaultMessage: The e-mail entered in the 'Receipients' field must be a valid e-mail address.
msgid "form_edit_invalid_to_email"
msgstr ""

#: components/ValidateConfigForm
# defaultMessage: Please, verify this configuration errors in sidebar. The form will be not displayed in view mode until this errors fields are not fixed.
msgid "form_edit_other_errors"
Expand Down
10 changes: 10 additions & 0 deletions locales/nl/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,16 @@ msgstr ""
msgid "form_edit_fill_required_configuration_fields"
msgstr ""

#: index
# defaultMessage: The e-mail entered in the 'From' field must be a valid e-mail address.
msgid "form_edit_invalid_from_email"
msgstr ""

#: index
# defaultMessage: The e-mail entered in the 'Receipients' field must be a valid e-mail address.
msgid "form_edit_invalid_to_email"
msgstr ""

#: components/ValidateConfigForm
# defaultMessage: Please, verify this configuration errors in sidebar. The form will be not displayed in view mode until this errors fields are not fixed.
msgid "form_edit_other_errors"
Expand Down
10 changes: 10 additions & 0 deletions locales/pt/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,16 @@ msgstr ""
msgid "form_edit_fill_required_configuration_fields"
msgstr ""

#: index
# defaultMessage: The e-mail entered in the 'From' field must be a valid e-mail address.
msgid "form_edit_invalid_from_email"
msgstr ""

#: index
# defaultMessage: The e-mail entered in the 'Receipients' field must be a valid e-mail address.
msgid "form_edit_invalid_to_email"
msgstr ""

#: components/ValidateConfigForm
# defaultMessage: Please, verify this configuration errors in sidebar. The form will be not displayed in view mode until this errors fields are not fixed.
msgid "form_edit_other_errors"
Expand Down
10 changes: 10 additions & 0 deletions locales/pt_BR/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,16 @@ msgstr "Exportar dados"
msgid "form_edit_fill_required_configuration_fields"
msgstr ""

#: index
# defaultMessage: The e-mail entered in the 'From' field must be a valid e-mail address.
msgid "form_edit_invalid_from_email"
msgstr ""

#: index
# defaultMessage: The e-mail entered in the 'Receipients' field must be a valid e-mail address.
msgid "form_edit_invalid_to_email"
msgstr ""

#: components/ValidateConfigForm
# defaultMessage: Please, verify this configuration errors in sidebar. The form will be not displayed in view mode until this errors fields are not fixed.
msgid "form_edit_other_errors"
Expand Down
10 changes: 10 additions & 0 deletions locales/ro/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,16 @@ msgstr ""
msgid "form_edit_fill_required_configuration_fields"
msgstr ""

#: index
# defaultMessage: The e-mail entered in the 'From' field must be a valid e-mail address.
msgid "form_edit_invalid_from_email"
msgstr ""

#: index
# defaultMessage: The e-mail entered in the 'Receipients' field must be a valid e-mail address.
msgid "form_edit_invalid_to_email"
msgstr ""

#: components/ValidateConfigForm
# defaultMessage: Please, verify this configuration errors in sidebar. The form will be not displayed in view mode until this errors fields are not fixed.
msgid "form_edit_other_errors"
Expand Down
12 changes: 11 additions & 1 deletion locales/volto.pot
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
msgid ""
msgstr ""
"Project-Id-Version: Plone\n"
"POT-Creation-Date: 2024-03-14T13:21:14.614Z\n"
"POT-Creation-Date: 2024-03-18T09:07:32.597Z\n"
"Last-Translator: Plone i18n <plone-i18n@lists.sourceforge.net>\n"
"Language-Team: Plone i18n <plone-i18n@lists.sourceforge.net>\n"
"MIME-Version: 1.0\n"
Expand Down Expand Up @@ -300,6 +300,16 @@ msgstr ""
msgid "form_edit_fill_required_configuration_fields"
msgstr ""

#: index
# defaultMessage: The e-mail entered in the 'From' field must be a valid e-mail address.
msgid "form_edit_invalid_from_email"
msgstr ""

#: index
# defaultMessage: The e-mail entered in the 'Receipients' field must be a valid e-mail address.
msgid "form_edit_invalid_to_email"
msgstr ""

#: components/ValidateConfigForm
# defaultMessage: Please, verify this configuration errors in sidebar. The form will be not displayed in view mode until this errors fields are not fixed.
msgid "form_edit_other_errors"
Expand Down
6 changes: 3 additions & 3 deletions src/components/ValidateConfigForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const messages = defineMessages({
},
});

const ValidateConfig = (data, blockSchema) => {
const ValidateConfig = (data, blockSchema, intl) => {
var SchemaValidators = config.blocks.blocksConfig.form.schemaValidators;

const required_fields = blockSchema.required;
Expand All @@ -32,7 +32,7 @@ const ValidateConfig = (data, blockSchema) => {
if (SchemaValidators) {
Object.keys(SchemaValidators).forEach((fieldName) => {
const validateFieldFN = SchemaValidators[fieldName];
const validation = validateFieldFN(data);
const validation = validateFieldFN(data, intl);

if (validation) {
schema_validation.push({ field: fieldName, message: validation });
Expand All @@ -54,7 +54,7 @@ const ValidateConfigForm = ({ data = {}, children, onEdit, onChangeBlock }) => {

useEffect(() => {
if (onEdit) {
const validation = ValidateConfig(data, blockSchema);
const validation = ValidateConfig(data, blockSchema, intl);
if (
JSON.stringify(validation) !== JSON.stringify(data.configValidation)
) {
Expand Down
1 change: 1 addition & 0 deletions src/fieldSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export default (props) => {
const schemaExtenderValues = schemaExtender
? schemaExtender(intl)
: { properties: [], fields: [], required: [] };
console.log(props);
return {
title: props?.label || '',
fieldsets: [
Expand Down
32 changes: 32 additions & 0 deletions src/helpers/validators.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
import { defineMessages } from 'react-intl';

const messages = defineMessages({
invalid_default_from: {
id: 'form_edit_invalid_from_email',
defaultMessage:
"The e-mail entered in the 'From' field must be a valid e-mail address.",
},
invalid_default_to: {
id: 'form_edit_invalid_to_email',
defaultMessage:
"The e-mail entered in the 'Receipients' field must be a valid e-mail address.",
},
});

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)
Expand All @@ -6,3 +21,20 @@ export const isValidEmail = (email) => {
const isValid = emailRegex.test(email);
return isValid;
};

export const validateDefaultFrom = (from, intl) => {
return isValidEmail(from)
? null
: intl.formatMessage(messages.invalid_default_from);
};

export const validateDefaultTo = (to, intl) => {
let valid = true;
let emails = to?.split(',').map((e) => e.replaceAll(' ', '')) ?? [];
emails.forEach((e) => {
if (!isValidEmail(e)) {
valid = false;
}
});
return valid ? null : intl.formatMessage(messages.invalid_default_to);
};
27 changes: 27 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import loadable from '@loadable/component';
import { defineMessages } from 'react-intl';
import formSVG from '@plone/volto/icons/form.svg';
import View from 'volto-form-block/components/View';
import Edit from 'volto-form-block/components/Edit';
Expand All @@ -20,11 +21,31 @@ import {
FromSchemaExtender,
HiddenSchemaExtender,
} from './components/FieldTypeSchemaExtenders';
import {
isValidEmail,
validateDefaultFrom,
validateDefaultTo,
} from 'volto-form-block/helpers/validators';

export {
submitForm,
getFormData,
exportCsvFormData,
} from 'volto-form-block/actions';
export { isValidEmail };

const messages = defineMessages({
invalid_default_from: {
id: 'form_edit_invalid_from_email',
defaultMessage:
"The e-mail entered in the 'From' field must be a valid e-mail address.",
},
invalid_default_to: {
id: 'form_edit_invalid_to_email',
defaultMessage:
"The e-mail entered in the 'Receipients' field must be a valid e-mail address.",
},
});

const applyConfig = (config) => {
config.blocks.blocksConfig = {
Expand All @@ -48,6 +69,12 @@ const applyConfig = (config) => {
},
schemaValidators: {
/*fieldname: validationFN(data)*/
default_from: (data, intl) => {
return validateDefaultFrom(data.default_from, intl);
},
default_to: (data, intl) => {
return validateDefaultTo(data.default_to, intl);
},
},
attachment_fields: ['attachment'],
restricted: false,
Expand Down

0 comments on commit ba41002

Please sign in to comment.