django-forms-frontend-validation 1.0.6__tar.gz → 1.0.7__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/MANIFEST.in +2 -2
  2. {django_forms_frontend_validation-1.0.6/django_forms_frontend_validation.egg-info → django_forms_frontend_validation-1.0.7}/PKG-INFO +12 -5
  3. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/README.md +11 -4
  4. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7/django_forms_frontend_validation.egg-info}/PKG-INFO +12 -5
  5. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/django_forms_frontend_validation.egg-info/SOURCES.txt +1 -7
  6. {django_forms_frontend_validation-1.0.6/formvalidator/static/dist → django_forms_frontend_validation-1.0.7/formvalidator/static/formvalidator/js}/forms.bundle.js +1 -1
  7. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/setup.py +1 -1
  8. django_forms_frontend_validation-1.0.6/formvalidator/static/webpack/package-lock.json +0 -2869
  9. django_forms_frontend_validation-1.0.6/formvalidator/static/webpack/package.json +0 -19
  10. django_forms_frontend_validation-1.0.6/formvalidator/static/webpack/src/css/style.css +0 -42
  11. django_forms_frontend_validation-1.0.6/formvalidator/static/webpack/src/js/formFunctions.js +0 -263
  12. django_forms_frontend_validation-1.0.6/formvalidator/static/webpack/src/js/index.js +0 -2
  13. django_forms_frontend_validation-1.0.6/formvalidator/static/webpack/webpack.config.js +0 -23
  14. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/LICENSE +0 -0
  15. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/django_forms_frontend_validation.egg-info/dependency_links.txt +0 -0
  16. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/django_forms_frontend_validation.egg-info/requires.txt +0 -0
  17. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/django_forms_frontend_validation.egg-info/top_level.txt +0 -0
  18. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/formvalidator/__init__.py +0 -0
  19. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/formvalidator/admin.py +0 -0
  20. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/formvalidator/apps.py +0 -0
  21. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/formvalidator/form_utils/__init__.py +0 -0
  22. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/formvalidator/form_utils/form_utils.py +0 -0
  23. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/formvalidator/forms.py +0 -0
  24. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/formvalidator/migrations/__init__.py +0 -0
  25. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/formvalidator/models.py +0 -0
  26. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/formvalidator/settings.py +0 -0
  27. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/formvalidator/templates/base.html +0 -0
  28. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/formvalidator/templates/formvalidator/sample.html +0 -0
  29. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/formvalidator/templates/formvalidator/sample2.html +0 -0
  30. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/formvalidator/templates/includes/header.html +0 -0
  31. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/formvalidator/tests.py +0 -0
  32. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/formvalidator/urls.py +0 -0
  33. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/formvalidator/views.py +0 -0
  34. {django_forms_frontend_validation-1.0.6 → django_forms_frontend_validation-1.0.7}/setup.cfg +0 -0
@@ -3,8 +3,8 @@ include LICENSE
3
3
  include README.md
4
4
 
5
5
  # Include specific static and template files
6
- recursive-include formvalidator/static/dist *
6
+ recursive-include formvalidator/static/formvalidator *
7
7
  recursive-include formvalidator/templates *
8
8
 
9
9
  # Exclude the node_modules directory
10
- prune formvalidator/static/webpack *
10
+ prune formvalidator/static/webpack
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: django-forms-frontend-validation
3
- Version: 1.0.6
3
+ Version: 1.0.7
4
4
  Summary: A Django app for front-end form validation
5
5
  Home-page: https://github.com/andrew-kyle92/django-forms-frontend-validation
6
6
  Author: Andrew Kyle
@@ -36,7 +36,7 @@ The application is designed to streamline the process of form validation, ensuri
36
36
  ## Usage
37
37
  ### Installation
38
38
  1. Install the Django project
39
- ```cmd
39
+ ```bash
40
40
  pip install django-frontend-forms-validation
41
41
  ```
42
42
  ### Setting Up
@@ -52,15 +52,17 @@ The application is designed to streamline the process of form validation, ensuri
52
52
  ```python
53
53
  from formvalidator.settings import *
54
54
 
55
- IGNORED_CLASSES = ['example-class', 'example-class-2', ...]
56
- IGNORE_VALIDATION = ['example-ignore-validation', ...]
55
+ IGNORED_CLASSES = ['example-class', 'example-class-2', ...] # replace these classes with your own
56
+ IGNORE_VALIDATION = ['example-ignore-validation', ...] # replace these classes with your own
57
57
  VALIDATE_ONLY_ON_SUBMIT = ['all'] # Options: "__all__", specific class names, or leave empty.
58
+ # validate only on submit will only validate the inputs when the submit button is clicked
59
+ # leaving it the list blank will allow for validation to happen on focus-out/onblur of an input
58
60
  ```
59
61
  2. Initial Forms:
60
62
  - Ensure the `_InitializeForms` method is called during page load to attach validation logic to forms dynamically.
61
63
  To your HTML template with the form, add this.
62
64
  ```html
63
- <script src="{% static 'dist/forms.bundle.js' %}"></script>
65
+ <script src="{% static 'formsvalidator/js/forms.bundle.js' %}"></script>
64
66
  <script>
65
67
  // fv (formsvalidator) is exported from forms.bundle.js
66
68
  window.addEventListener("load", () => {
@@ -76,6 +78,11 @@ The application is designed to streamline the process of form validation, ensuri
76
78
  });
77
79
  </script>
78
80
  ```
81
+ **Quick Note* - if your template is not finding `'formvalidator/js/forms.bundle.js'`, make sure either `STATICFILES_DIRS` is defined within your settings.py files, or if on production `STATIC_ROOT` is defined. If `STATIC_ROOT` is defined then make sure to run:
82
+ ```bash
83
+ ./manage.py collectstatic
84
+ ```
85
+
79
86
  3. Server-Side Context:
80
87
  - Use the `FormsValidator` class to pass configuration to templates:
81
88
  ```python
@@ -21,7 +21,7 @@ The application is designed to streamline the process of form validation, ensuri
21
21
  ## Usage
22
22
  ### Installation
23
23
  1. Install the Django project
24
- ```cmd
24
+ ```bash
25
25
  pip install django-frontend-forms-validation
26
26
  ```
27
27
  ### Setting Up
@@ -37,15 +37,17 @@ The application is designed to streamline the process of form validation, ensuri
37
37
  ```python
38
38
  from formvalidator.settings import *
39
39
 
40
- IGNORED_CLASSES = ['example-class', 'example-class-2', ...]
41
- IGNORE_VALIDATION = ['example-ignore-validation', ...]
40
+ IGNORED_CLASSES = ['example-class', 'example-class-2', ...] # replace these classes with your own
41
+ IGNORE_VALIDATION = ['example-ignore-validation', ...] # replace these classes with your own
42
42
  VALIDATE_ONLY_ON_SUBMIT = ['all'] # Options: "__all__", specific class names, or leave empty.
43
+ # validate only on submit will only validate the inputs when the submit button is clicked
44
+ # leaving it the list blank will allow for validation to happen on focus-out/onblur of an input
43
45
  ```
44
46
  2. Initial Forms:
45
47
  - Ensure the `_InitializeForms` method is called during page load to attach validation logic to forms dynamically.
46
48
  To your HTML template with the form, add this.
47
49
  ```html
48
- <script src="{% static 'dist/forms.bundle.js' %}"></script>
50
+ <script src="{% static 'formsvalidator/js/forms.bundle.js' %}"></script>
49
51
  <script>
50
52
  // fv (formsvalidator) is exported from forms.bundle.js
51
53
  window.addEventListener("load", () => {
@@ -61,6 +63,11 @@ The application is designed to streamline the process of form validation, ensuri
61
63
  });
62
64
  </script>
63
65
  ```
66
+ **Quick Note* - if your template is not finding `'formvalidator/js/forms.bundle.js'`, make sure either `STATICFILES_DIRS` is defined within your settings.py files, or if on production `STATIC_ROOT` is defined. If `STATIC_ROOT` is defined then make sure to run:
67
+ ```bash
68
+ ./manage.py collectstatic
69
+ ```
70
+
64
71
  3. Server-Side Context:
65
72
  - Use the `FormsValidator` class to pass configuration to templates:
66
73
  ```python
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: django-forms-frontend-validation
3
- Version: 1.0.6
3
+ Version: 1.0.7
4
4
  Summary: A Django app for front-end form validation
5
5
  Home-page: https://github.com/andrew-kyle92/django-forms-frontend-validation
6
6
  Author: Andrew Kyle
@@ -36,7 +36,7 @@ The application is designed to streamline the process of form validation, ensuri
36
36
  ## Usage
37
37
  ### Installation
38
38
  1. Install the Django project
39
- ```cmd
39
+ ```bash
40
40
  pip install django-frontend-forms-validation
41
41
  ```
42
42
  ### Setting Up
@@ -52,15 +52,17 @@ The application is designed to streamline the process of form validation, ensuri
52
52
  ```python
53
53
  from formvalidator.settings import *
54
54
 
55
- IGNORED_CLASSES = ['example-class', 'example-class-2', ...]
56
- IGNORE_VALIDATION = ['example-ignore-validation', ...]
55
+ IGNORED_CLASSES = ['example-class', 'example-class-2', ...] # replace these classes with your own
56
+ IGNORE_VALIDATION = ['example-ignore-validation', ...] # replace these classes with your own
57
57
  VALIDATE_ONLY_ON_SUBMIT = ['all'] # Options: "__all__", specific class names, or leave empty.
58
+ # validate only on submit will only validate the inputs when the submit button is clicked
59
+ # leaving it the list blank will allow for validation to happen on focus-out/onblur of an input
58
60
  ```
59
61
  2. Initial Forms:
60
62
  - Ensure the `_InitializeForms` method is called during page load to attach validation logic to forms dynamically.
61
63
  To your HTML template with the form, add this.
62
64
  ```html
63
- <script src="{% static 'dist/forms.bundle.js' %}"></script>
65
+ <script src="{% static 'formsvalidator/js/forms.bundle.js' %}"></script>
64
66
  <script>
65
67
  // fv (formsvalidator) is exported from forms.bundle.js
66
68
  window.addEventListener("load", () => {
@@ -76,6 +78,11 @@ The application is designed to streamline the process of form validation, ensuri
76
78
  });
77
79
  </script>
78
80
  ```
81
+ **Quick Note* - if your template is not finding `'formvalidator/js/forms.bundle.js'`, make sure either `STATICFILES_DIRS` is defined within your settings.py files, or if on production `STATIC_ROOT` is defined. If `STATIC_ROOT` is defined then make sure to run:
82
+ ```bash
83
+ ./manage.py collectstatic
84
+ ```
85
+
79
86
  3. Server-Side Context:
80
87
  - Use the `FormsValidator` class to pass configuration to templates:
81
88
  ```python
@@ -19,13 +19,7 @@ formvalidator/views.py
19
19
  formvalidator/form_utils/__init__.py
20
20
  formvalidator/form_utils/form_utils.py
21
21
  formvalidator/migrations/__init__.py
22
- formvalidator/static/dist/forms.bundle.js
23
- formvalidator/static/webpack/package-lock.json
24
- formvalidator/static/webpack/package.json
25
- formvalidator/static/webpack/webpack.config.js
26
- formvalidator/static/webpack/src/css/style.css
27
- formvalidator/static/webpack/src/js/formFunctions.js
28
- formvalidator/static/webpack/src/js/index.js
22
+ formvalidator/static/formvalidator/js/forms.bundle.js
29
23
  formvalidator/templates/base.html
30
24
  formvalidator/templates/formvalidator/sample.html
31
25
  formvalidator/templates/formvalidator/sample2.html
@@ -126,7 +126,7 @@ eval("\n\n/* istanbul ignore next */\nfunction styleTagTransform(css, styleElem
126
126
  \*********************************/
127
127
  /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
128
128
 
129
- eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ _Initialize: () => (/* binding */ _Initialize),\n/* harmony export */ _InitializeForms: () => (/* binding */ _InitializeForms),\n/* harmony export */ checkForm: () => (/* binding */ checkForm),\n/* harmony export */ csrftoken: () => (/* binding */ csrftoken),\n/* harmony export */ fetchHandleForm: () => (/* binding */ fetchHandleForm)\n/* harmony export */ });\n// ########## Getting the csrf token for the fetch calls ##########\r\nfunction getCookie(name) {\r\n let cookieValue = null;\r\n if (document.cookie && document.cookie !== '') {\r\n const cookies = document.cookie.split(';');\r\n for (let i = 0; i < cookies.length; i++) {\r\n const cookie = cookies[i].trim();\r\n // Does this cookie string begin with the name we want?\r\n if (cookie.substring(0, name.length + 1) === (name + '=')) {\r\n cookieValue = decodeURIComponent(cookie.substring(name.length + 1));\r\n break;\r\n }\r\n }\r\n }\r\n return cookieValue;\r\n}\r\nconst csrftoken = getCookie('csrftoken');\r\n\r\n// ##### Fetch Calls #####\r\n// performs fetch call to view\r\nconst fetchHandleForm = async (form) => {\r\n let formData = new FormData(form);\r\n return await fetch(form.action, {\r\n method: \"POST\",\r\n credentials: \"same-origin\",\r\n headers: {\r\n // \"Accept\": \"m\",\r\n // \"X-Requested-With\": \"XMLHttpRequest\",\r\n \"X-CSRFToken\": csrftoken,\r\n },\r\n body: formData,\r\n }).then(async (response) => {\r\n return response.json();\r\n });\r\n}\r\n// ***** Adding asterisks to labels of required inputs *****\r\nfunction addAsterisks(form) {\r\n // gathering all inputs\r\n let formGroups = document.querySelectorAll(`#${form.id} .form-group`);\r\n let inputs = getRequiredFields(formGroups);\r\n\r\n // adding the required-field class which will add the asterisk\r\n for (let i = 0; i < inputs.length; i++) {\r\n let label = document.querySelector(`label[for=${inputs[i].name}]`);\r\n if (inputs[i].required) {\r\n label.classList.add(\"required-field\");\r\n }\r\n }\r\n}\r\n\r\n// In-line validation on required fields\r\nfunction validateInputs(form) {\r\n // gathering all inputs\r\n let formGroups = document.querySelectorAll(`#${form.id} .form-group`);\r\n let inputs = getRequiredFields(formGroups);\r\n\r\n // adding listeners to each input for validation\r\n for (let i = 0; i < inputs.length; i++) {\r\n inputs[i].addEventListener(\"focusout\", () => {\r\n if (inputs[i].value === \"\" || inputs[i].value === null) {\r\n addError(inputs[i]);\r\n } else {\r\n removeError(inputs[i]);\r\n }\r\n });\r\n }\r\n}\r\n// validateInputs();\r\n\r\n// check form function\r\nfunction checkForm(form) {\r\n let errors = false;\r\n\r\n // gathering all inputs\r\n let formGroups = document.querySelectorAll(`#${form.id} .form-group`);\r\n let inputs = getRequiredFields(formGroups);\r\n\r\n // iterating through all required fields to check for invalidation\r\n for (let i = 0; i < inputs.length; i++) {\r\n let input = inputs[i];\r\n if (input.value === \"\" || !input.value) {\r\n addError(input);\r\n errors = true;\r\n }\r\n }\r\n\r\n return errors\r\n}\r\n\r\n// submit button validation check\r\nfunction submitValidation(form) {\r\n form.form.addEventListener(\"submit\", (e) => {\r\n // preventing default submission behavior\r\n e.preventDefault();\r\n\r\n // gathering all inputs\r\n let f = e.target;\r\n // let formGroups = document.querySelectorAll(`#${form.id} .form-group`);\r\n // let inputs = getRequiredFields(formGroups);\r\n // // let form = document.getElementById(\"form\") !== null ? document.getElementById(\"form\") : document.getElementById(\"userForm\");\r\n let errors = checkForm(f);\r\n\r\n // submitting the form if there aren't any errors\r\n if (!errors) {\r\n form.form.submit();\r\n } else {\r\n let invalidInputs = document.getElementsByClassName(\"validation-error\");\r\n // scroll to the first invalid input\r\n invalidInputs[0].parentElement.scrollIntoView();\r\n }\r\n });\r\n}\r\n// submitValidation();\r\n\r\n// adds an error to an input\r\nfunction addError(input) {\r\n let inputParent = input.parentElement;\r\n if (!inputParent.className.includes(\"form-group\")){\r\n inputParent = input.parentElement.parentElement;\r\n }\r\n let errorAdded = inputParent.dataset.errorAdded;\r\n inputParent.classList.add(\"validation-error\");\r\n input.classList.add(\"error-input\")\r\n if (errorAdded === \"false\" || !errorAdded) {\r\n // creating the error message p\r\n let eP = document.createElement(\"p\");\r\n eP.setAttribute(\"class\", \"error-msg\");\r\n eP.innerText = \"This input is required\";\r\n inputParent.appendChild(eP);\r\n inputParent.dataset.errorAdded = \"true\";\r\n }\r\n}\r\n\r\n// removes the error from an input\r\nfunction removeError(input) {\r\n let inputParent = input.parentElement;\r\n if (!inputParent.className.includes(\"form-group\")){\r\n inputParent = input.parentElement.parentElement;\r\n }\r\n let errorAdded = inputParent.dataset.errorAdded;\r\n inputParent.classList.remove(\"validation-error\");\r\n input.classList.remove(\"error-input\");\r\n // removing the error message p\r\n if (errorAdded === \"true\") {\r\n inputParent.removeChild(inputParent.lastElementChild);\r\n inputParent.dataset.errorAdded = \"false\";\r\n }\r\n}\r\n\r\n// function to get all required input\r\nfunction getRequiredFields(formGroups) {\r\n let nameList = [\"SELECT\", \"INPUT\", \"TEXTAREA\"];\r\n let inputs = [];\r\n // let formGroups = document.getElementsByClassName(\"form-group\");\r\n for (let i = 0; i < formGroups.length; i++) {\r\n let children = formGroups[i].children;\r\n for (let j = 0; j < children.length; j++) {\r\n if (children[j].tagName === \"DIV\"){\r\n let grandChildren = children[j].children;\r\n for (let a = 0; a < grandChildren.length; a++) {\r\n if (nameList.includes(grandChildren[a].tagName)) {\r\n if (grandChildren[a].required) {\r\n inputs.push(grandChildren[a]);\r\n }\r\n }\r\n }\r\n }\r\n else{\r\n if (nameList.includes(children[j].tagName)) {\r\n if (children[j].required) {\r\n inputs.push(children[j]);\r\n }\r\n }\r\n }\r\n }\r\n }\r\n return inputs\r\n}\r\n\r\n// checks if the form will be ignored form validation.\r\nfunction getIgnored(form, ignoredClasses) {\r\n let isIgnored = false;\r\n let classes = form.classList;\r\n if (ignoredClasses.length > 0) {\r\n classes.forEach((_class) => {\r\n if (ignoredClasses.includes(_class)) {\r\n isIgnored = true;\r\n return isIgnored;\r\n }\r\n });\r\n }\r\n return isIgnored\r\n}\r\n\r\n// Checks if the form will be validated.\r\nfunction isValidate(form, ignoredClasses) {\r\n let enableValidate = true;\r\n let classes = form.classList;\r\n if (ignoredClasses.length > 0) {\r\n classes.forEach((_class) => {\r\n if (ignoredClasses.includes(_class)) {\r\n enableValidate = false;\r\n return enableValidate;\r\n }\r\n });\r\n }\r\n return enableValidate\r\n}\r\n\r\n// Confirms if a form will only validate the form on submit, only.\r\nfunction getValidateOnlyValidateOnSubmit(form, validateOnlyOnSubmit, enableValidation) {\r\n let validateOnSubmit = false;\r\n let trueFlags = [\"all\", \"__all__\", \"*\", \"true\"];\r\n if (enableValidation) {\r\n let classes = form.classList;\r\n if (trueFlags.includes(validateOnlyOnSubmit[0])) {\r\n validateOnSubmit = true;\r\n return true;\r\n }\r\n else {\r\n classes.forEach((_class) => {\r\n if (validateOnlyOnSubmit.includes(_class)) {\r\n validateOnSubmit = true;\r\n return validateOnlyOnSubmit;\r\n }\r\n });\r\n }\r\n }\r\n return validateOnSubmit;\r\n}\r\n\r\n// adds listener logic to the form\r\nfunction _Initialize(form={}){\r\n if (!form.ignored || form.enableValidation) {\r\n // add all listeners to each form\r\n addAsterisks(form);\r\n if (!form.validateOnlyOnSubmit) {\r\n validateInputs(form);\r\n }\r\n if (!form.ignored) {\r\n submitValidation(form);\r\n }\r\n }\r\n}\r\n\r\n// function to initialize forms into a json object\r\nfunction _InitializeForms(formNodes, ignoredFormClasses=[], ignoredValidationClasses=[], validateOnlyOnSubmit){\r\n for (let i = 0; i < formNodes.length; i++) {\r\n let currentForm = formNodes[i];\r\n let ignored = getIgnored(currentForm, ignoredFormClasses);\r\n let enableValidation = isValidate(currentForm, ignoredValidationClasses);\r\n let validateOnSubmit = getValidateOnlyValidateOnSubmit(currentForm, validateOnlyOnSubmit, enableValidation);\r\n let _form = {\r\n id: currentForm.id,\r\n form: currentForm,\r\n ignored: ignored,\r\n enableValidation: enableValidation,\r\n validateOnlyOnSubmit: validateOnSubmit,\r\n }\r\n\r\n // adding form functionality\r\n _Initialize(_form);\r\n }\r\n}\n\n//# sourceURL=webpack://fv/./src/js/formFunctions.js?");
129
+ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ _Initialize: () => (/* binding */ _Initialize),\n/* harmony export */ _InitializeForms: () => (/* binding */ _InitializeForms),\n/* harmony export */ checkForm: () => (/* binding */ checkForm),\n/* harmony export */ csrftoken: () => (/* binding */ csrftoken),\n/* harmony export */ fetchHandleForm: () => (/* binding */ fetchHandleForm)\n/* harmony export */ });\n// ########## Getting the csrf token for the fetch calls ##########\r\nfunction getCookie(name) {\r\n let cookieValue = null;\r\n if (document.cookie && document.cookie !== '') {\r\n const cookies = document.cookie.split(';');\r\n for (let i = 0; i < cookies.length; i++) {\r\n const cookie = cookies[i].trim();\r\n // Does this cookie string begin with the name we want?\r\n if (cookie.substring(0, name.length + 1) === (name + '=')) {\r\n cookieValue = decodeURIComponent(cookie.substring(name.length + 1));\r\n break;\r\n }\r\n }\r\n }\r\n return cookieValue;\r\n}\r\nconst csrftoken = getCookie('csrftoken');\r\n\r\n// ##### Fetch Calls #####\r\n// performs fetch call to view\r\nconst fetchHandleForm = async (form) => {\r\n let formData = new FormData(form);\r\n return await fetch(form.action, {\r\n method: \"POST\",\r\n credentials: \"same-origin\",\r\n headers: {\r\n // \"Accept\": \"m\",\r\n // \"X-Requested-With\": \"XMLHttpRequest\",\r\n \"X-CSRFToken\": csrftoken,\r\n },\r\n body: formData,\r\n }).then(async (response) => {\r\n return response.json();\r\n });\r\n}\r\n// ***** Adding asterisks to labels of required inputs *****\r\nfunction addAsterisks(form) {\r\n // gathering all inputs\r\n let formGroups = document.querySelectorAll(`#${form.id} .form-group`);\r\n let inputs = getRequiredFields(formGroups);\r\n\r\n // adding the required-field class which will add the asterisk\r\n for (let i = 0; i < inputs.length; i++) {\r\n let label = document.querySelector(`label[for=${inputs[i].name}]`);\r\n if (inputs[i].required) {\r\n label.classList.add(\"required-field\");\r\n }\r\n }\r\n}\r\n\r\n// In-line validation on required fields\r\nfunction validateInputs(form) {\r\n // gathering all inputs\r\n let formGroups = document.querySelectorAll(`#${form.id} .form-group`);\r\n let inputs = getRequiredFields(formGroups);\r\n\r\n // adding listeners to each input for validation\r\n for (let i = 0; i < inputs.length; i++) {\r\n inputs[i].addEventListener(\"focusout\", () => {\r\n if (inputs[i].value === \"\" || inputs[i].value === null) {\r\n addError(inputs[i]);\r\n } else {\r\n removeError(inputs[i]);\r\n }\r\n });\r\n }\r\n}\r\n// validateInputs();\r\n\r\n// check form function\r\nfunction checkForm(form) {\r\n let errors = false;\r\n\r\n // gathering all inputs\r\n let formGroups = document.querySelectorAll(`#${form.id} .form-group`);\r\n let inputs = getRequiredFields(formGroups);\r\n\r\n // iterating through all required fields to check for invalidation\r\n for (let i = 0; i < inputs.length; i++) {\r\n let input = inputs[i];\r\n if (input.value === \"\" || !input.value) {\r\n addError(input);\r\n errors = true;\r\n }\r\n }\r\n\r\n return errors\r\n}\r\n\r\n// submit button validation check\r\nfunction submitValidation(form) {\r\n form.form.addEventListener(\"submit\", (e) => {\r\n // preventing default submission behavior\r\n e.preventDefault();\r\n\r\n // gathering all inputs\r\n let f = e.target;\r\n // let formGroups = document.querySelectorAll(`#${form.id} .form-group`);\r\n // let inputs = getRequiredFields(formGroups);\r\n // // let form = document.getElementById(\"form\") !== null ? document.getElementById(\"form\") : document.getElementById(\"userForm\");\r\n let errors = checkForm(f);\r\n\r\n // submitting the form if there aren't any errors\r\n if (!errors) {\r\n form.form.submit();\r\n } else {\r\n let invalidInputs = document.getElementsByClassName(\"validation-error\");\r\n // scroll to the first invalid input\r\n invalidInputs[0].parentElement.scrollIntoView();\r\n }\r\n });\r\n}\r\n\r\n// adds an error to an input\r\nfunction addError(input) {\r\n let inputParent = input.parentElement;\r\n if (!inputParent.className.includes(\"form-group\")){\r\n inputParent = input.parentElement.parentElement;\r\n }\r\n let errorAdded = inputParent.dataset.errorAdded;\r\n inputParent.classList.add(\"validation-error\");\r\n input.classList.add(\"error-input\")\r\n if (errorAdded === \"false\" || !errorAdded) {\r\n // creating the error message p\r\n let eP = document.createElement(\"p\");\r\n eP.setAttribute(\"class\", \"error-msg\");\r\n eP.innerText = \"This input is required\";\r\n inputParent.appendChild(eP);\r\n inputParent.dataset.errorAdded = \"true\";\r\n }\r\n}\r\n\r\n// removes the error from an input\r\nfunction removeError(input) {\r\n let inputParent = input.parentElement;\r\n if (!inputParent.className.includes(\"form-group\")){\r\n inputParent = input.parentElement.parentElement;\r\n }\r\n let errorAdded = inputParent.dataset.errorAdded;\r\n inputParent.classList.remove(\"validation-error\");\r\n input.classList.remove(\"error-input\");\r\n // removing the error message p\r\n if (errorAdded === \"true\") {\r\n inputParent.removeChild(inputParent.lastElementChild);\r\n inputParent.dataset.errorAdded = \"false\";\r\n }\r\n}\r\n\r\n// function to get all required input\r\nfunction getRequiredFields(formGroups) {\r\n let nameList = [\"SELECT\", \"INPUT\", \"TEXTAREA\"];\r\n let inputs = [];\r\n // let formGroups = document.getElementsByClassName(\"form-group\");\r\n for (let i = 0; i < formGroups.length; i++) {\r\n let children = formGroups[i].children;\r\n for (let j = 0; j < children.length; j++) {\r\n if (children[j].tagName === \"DIV\"){\r\n let grandChildren = children[j].children;\r\n for (let a = 0; a < grandChildren.length; a++) {\r\n if (nameList.includes(grandChildren[a].tagName)) {\r\n if (grandChildren[a].required) {\r\n inputs.push(grandChildren[a]);\r\n }\r\n }\r\n }\r\n }\r\n else{\r\n if (nameList.includes(children[j].tagName)) {\r\n if (children[j].required) {\r\n inputs.push(children[j]);\r\n }\r\n }\r\n }\r\n }\r\n }\r\n return inputs\r\n}\r\n\r\n// checks if the form will be ignored form validation.\r\nfunction getIgnored(form, ignoredClasses) {\r\n let isIgnored = false;\r\n let classes = form.classList;\r\n if (ignoredClasses.length > 0) {\r\n classes.forEach((_class) => {\r\n if (ignoredClasses.includes(_class)) {\r\n isIgnored = true;\r\n return isIgnored;\r\n }\r\n });\r\n }\r\n return isIgnored\r\n}\r\n\r\n// Checks if the form will be validated.\r\nfunction isValidate(form, ignoredClasses) {\r\n let enableValidate = true;\r\n let classes = form.classList;\r\n if (ignoredClasses.length > 0) {\r\n classes.forEach((_class) => {\r\n if (ignoredClasses.includes(_class)) {\r\n enableValidate = false;\r\n return enableValidate;\r\n }\r\n });\r\n }\r\n return enableValidate\r\n}\r\n\r\n// Confirms if a form will only validate the form on submit, only.\r\nfunction getValidateOnlyValidateOnSubmit(form, validateOnlyOnSubmit, enableValidation) {\r\n let validateOnSubmit = false;\r\n let trueFlags = [\"all\", \"__all__\", \"*\", \"true\"];\r\n if (enableValidation) {\r\n let classes = form.classList;\r\n if (trueFlags.includes(validateOnlyOnSubmit[0])) {\r\n validateOnSubmit = true;\r\n return true;\r\n }\r\n else {\r\n classes.forEach((_class) => {\r\n if (validateOnlyOnSubmit.includes(_class)) {\r\n validateOnSubmit = true;\r\n return validateOnlyOnSubmit;\r\n }\r\n });\r\n }\r\n }\r\n return validateOnSubmit;\r\n}\r\n\r\n// adds listener logic to the form\r\nfunction _Initialize(form={}){\r\n if (!form.ignored || form.enableValidation) {\r\n // add all listeners to each form\r\n addAsterisks(form);\r\n if (!form.validateOnlyOnSubmit) {\r\n validateInputs(form);\r\n }\r\n if (!form.ignored) {\r\n submitValidation(form);\r\n }\r\n }\r\n}\r\n\r\n// function to initialize forms into a json object\r\nfunction _InitializeForms(formNodes, ignoredFormClasses=[], ignoredValidationClasses=[], validateOnlyOnSubmit){\r\n for (let i = 0; i < formNodes.length; i++) {\r\n let currentForm = formNodes[i];\r\n let ignored = getIgnored(currentForm, ignoredFormClasses);\r\n let enableValidation = isValidate(currentForm, ignoredValidationClasses);\r\n let validateOnSubmit = getValidateOnlyValidateOnSubmit(currentForm, validateOnlyOnSubmit, enableValidation);\r\n let _form = {\r\n id: currentForm.id,\r\n form: currentForm,\r\n ignored: ignored,\r\n enableValidation: enableValidation,\r\n validateOnlyOnSubmit: validateOnSubmit,\r\n }\r\n\r\n // adding form functionality\r\n _Initialize(_form);\r\n }\r\n}\n\n//# sourceURL=webpack://fv/./src/js/formFunctions.js?");
130
130
 
131
131
  /***/ })
132
132
 
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
2
2
 
3
3
  setup(
4
4
  name='django-forms-frontend-validation',
5
- version='1.0.6',
5
+ version='1.0.7',
6
6
  packages=find_packages(exclude=['core*']),
7
7
  include_package_data=True,
8
8
  license='MIT', # Use the license that applies to your project