summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/directives/validation.js
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2021-05-12 09:10:19 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2021-05-12 09:10:19 +0000
commit133ec9237af290062aae70e6f115db69b51c88de (patch)
tree866ddeec2098e6557e4fe93941438ada45713940 /app/assets/javascripts/vue_shared/directives/validation.js
parentf9ad7e0c9fa576bf4d651ffe3e278750bf548400 (diff)
downloadgitlab-ce-133ec9237af290062aae70e6f115db69b51c88de.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/assets/javascripts/vue_shared/directives/validation.js')
-rw-r--r--app/assets/javascripts/vue_shared/directives/validation.js56
1 files changed, 56 insertions, 0 deletions
diff --git a/app/assets/javascripts/vue_shared/directives/validation.js b/app/assets/javascripts/vue_shared/directives/validation.js
index df853a7c5b5..692f2769b88 100644
--- a/app/assets/javascripts/vue_shared/directives/validation.js
+++ b/app/assets/javascripts/vue_shared/directives/validation.js
@@ -136,3 +136,59 @@ export default function initValidation(customFeedbackMap = {}) {
},
};
}
+
+/**
+ * This is a helper that initialize the form fields structure to be used in initForm
+ * @param {*} fieldValues
+ * @returns formObject
+ */
+const initFormField = ({ value, required = true, skipValidation = false }) => ({
+ value,
+ required,
+ state: skipValidation ? true : null,
+ feedback: null,
+});
+
+/**
+ * This is a helper that initialize the form structure that is compliant to be used with the validation directive
+ *
+ * @example
+ * const form initForm = initForm({
+ * fields: {
+ * name: {
+ * value: 'lorem'
+ * },
+ * description: {
+ * value: 'ipsum',
+ * required: false,
+ * skipValidation: true
+ * }
+ * }
+ * })
+ *
+ * @example
+ * const form initForm = initForm({
+ * state: true, // to override
+ * foo: { // something custom
+ * bar: 'lorem'
+ * },
+ * fields: {...}
+ * })
+ *
+ * @param {*} formObject
+ * @returns form
+ */
+export const initForm = ({ fields = {}, ...rest } = {}) => {
+ const initFields = Object.fromEntries(
+ Object.entries(fields).map(([fieldName, fieldValues]) => {
+ return [fieldName, initFormField(fieldValues)];
+ }),
+ );
+
+ return {
+ state: false,
+ showValidation: false,
+ ...rest,
+ fields: initFields,
+ };
+};