Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Neoletter | Form Builder | Subscription as a hidden field #542

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Widgets/FormHiddenFieldWidget/FormHiddenFieldWidgetClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ export const FormHiddenFieldWidget = Scrivito.provideWidgetClass(
{
onlyInside: "FormContainerWidget",
attributes: {
type: [
"enum",
{
values: ["custom"].concat(
process.env.ENABLE_NEOLETTER_FORM_BUILDER_SUBSCRIPTION_FEATURE
? ["subscription"]
: []
),
},
],
customFieldName: "string",
hiddenValue: "string",
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
import * as Scrivito from "scrivito";
import formHiddenFieldWidgetIcon from "../../assets/images/form_hidden_field_widget.svg";
import { customFieldNameValidation } from "../FormContainerWidget/utils/validations/customFieldNameValidation";
import { isCustomType } from "../FormContainerWidget/utils/isCustomType";
import { typeValidation } from "../FormContainerWidget/utils/validations/typeValidation";
import { getFieldName } from "../FormContainerWidget/utils/getFieldName";

Scrivito.provideEditingConfig("FormHiddenFieldWidget", {
title: "Hidden Form Field",
thumbnail: formHiddenFieldWidgetIcon,
attributes: {
type: {
title: "Input type",
values: [
{ value: "custom", title: "Custom" },
...(process.env.ENABLE_NEOLETTER_FORM_BUILDER_SUBSCRIPTION_FEATURE
? [{ value: "subscription", title: "Subscription" }]
: []),
],
},
customFieldName: { title: "Field name" },
hiddenValue: {
title: "Hidden value",
description: "This value is sent on every submission of the form.",
},
},
properties: ["customFieldName", "hiddenValue"],
properties: (widget) => {
if (process.env.ENABLE_NEOLETTER_FORM_BUILDER_SUBSCRIPTION_FEATURE) {
if (!isCustomType(widget)) {
return ["type", "hiddenValue"];
}

return ["type", "customFieldName", "hiddenValue"];
}

return ["customFieldName", "hiddenValue"];
},
initialContent: {
type: "custom",
customFieldName: "custom_hidden_field",
},
validations: [
Expand All @@ -34,12 +57,32 @@ Scrivito.provideEditingConfig("FormHiddenFieldWidget", {
severity: "info",
};
},
],
titleForContent: (widget) =>
`Hidden Form Field: ${[
widget.get("customFieldName"),
widget.get("hiddenValue"),
]
].concat(
process.env.ENABLE_NEOLETTER_FORM_BUILDER_SUBSCRIPTION_FEATURE
? [
typeValidation,
[
"hiddenValue",
(hiddenValue, { widget }) => {
const fieldName = getFieldName(widget);

if (fieldName === "subscription" && hiddenValue !== "on") {
return {
message:
"Please enter 'on' to activate the subscription process on every submission.",
severity: "warning",
};
}
},
],
]
: []
),
titleForContent: (widget) => {
const fieldName = getFieldName(widget);

return `Hidden Form Field: ${[fieldName, widget.get("hiddenValue")]
.filter((e) => e)
.join(" - ")}`,
.join(" - ")}`;
},
});