A Django app for front-end form validation
Project description
django-forms-frontend-validation App
This project provides a comprehensive system for handling and validating HTML forms in Django applications. It combines client-side JavaScript-based validation and server-side Python logic for robust form processing.
The application is designed to streamline the process of form validation, ensuring user inputs meet the requirements before submitting them to the server. The system offers features like automatic required-field validation, error handling, and dynamic CSRF token management for secure data transmission.
Features
- Client-Side Validation:
- Automatically validates required fields.
- Displays validation errors inline and dynamically updates them upon correction.
- Adds asterisks to labels of required fields for better user clarity.
- Server-Side Settings:
- Control which forms and fields to ignore during validation.
- Define validation behavior, such as enforcing checks only on form submission.
- Integration with Django Settings:
- Use Django's settings to dynamically configure validation rules.
- Secure Fetch Calls:
- Includes CSRF token management for secure AJAX-based form submissions.
Usage
Installation
- Install the Django project
pip install django-frontend-forms-validation
Setting Up
-
Define Settings in settings.py
- Add
formvalidatorto installed apps.INSTALLED_APPS = [ ..., 'formvalidator', ]
- Configure the following variables to customize the behavior, after importing the form settings.
from formvalidator.settings import * IGNORED_CLASSES = ['example-class', 'example-class-2', ...] # replace these classes with your own IGNORE_VALIDATION = ['example-ignore-validation', ...] # replace these classes with your own VALIDATE_ONLY_ON_SUBMIT = ['all'] # Options: "__all__", specific class names, or leave empty. # validate only on submit will only validate the inputs when the submit button is clicked # leaving it the list blank will allow for validation to happen on focus-out/onblur of an input
- Add
-
Initial Forms:
- Ensure the
_InitializeFormsmethod is called during page load to attach validation logic to forms dynamically. To your HTML template with the form, add this.
<script src="{% static 'formsvalidator/js/forms.bundle.js' %}"></script> <script> // fv (formsvalidator) is exported from forms.bundle.js window.addEventListener("load", () => { let ignoredClasses = {{ form_validator.ignored_classes|safe }}; // add more classes that represent forms you want this script to ignore. let ignoreValidation = {{ form_validator.ignore_validation|safe }}; // add any form classes that you want to ignore validation let validateOnlyOnSubmit = {{ form_validator.validate_only_on_submit|safe }}; // for hitting all forms make index 0 either __all__, all, * or leave blank for false or use false let forms = document.getElementsByTagName("form"); // if (form || userForm) { if (forms.length > 0) { // calling specific functions on all forms fv._InitializeForms(forms, ignoredClasses, ignoreValidation, validateOnlyOnSubmit); } }); </script>
*Quick Note - if your template is not finding
'formvalidator/js/forms.bundle.js', make sure eitherSTATICFILES_DIRSis defined within your settings.py files, or if on productionSTATIC_ROOTis defined. IfSTATIC_ROOTis defined then make sure to run:./manage.py collectstatic - Ensure the
-
Server-Side Context:
- Use the
FormsValidatorclass to pass configuration to templates:
from formvalidator.form_utils import FormsValidator def my_view(request): form_validator = FormsValidator() context = { 'form_validator': form_validator.get_context(), } return render(request, 'my_template.html', context)
- Use the
-
Add
divGroups to the HTML Form:- The JavaScript in this project relies on each form field being wrapped inside an outer div with the classname of
form-group. - It helps set apart each input from other inputs within the form.
- Here is an example of the setup:
<form ...> {% csrf_token %} <div class="form-group"> <label for="field1">Field 1</label> <input type="text" name="field1"> </div> <div class="form-group"> <label for="field2">Field 1</label> <input type="text" name="field2"> </div> <-- Adding the rest of the form groups below --> ... </form>
- If iterating through each form input using the
formcontext variable:
<form ...> {% csrf_token %} <-- iterating through each form field --> {% for field in form %} <div class="form-group"> <label for="{{ field.name }}">{{ field.label }}</label> {{ field }} </div> {% endfor %} </form>
- The JavaScript in this project relies on each form field being wrapped inside an outer div with the classname of
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_forms_frontend_validation-1.0.8.tar.gz.
File metadata
- Download URL: django_forms_frontend_validation-1.0.8.tar.gz
- Upload date:
- Size: 16.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7ccda9f5bae0d00bc7ac6e1a4b944b46170babe6a212b9db56eeb70e94a0a68c
|
|
| MD5 |
08a02ecb8f2c961c47302f3e6f83cc78
|
|
| BLAKE2b-256 |
906ef5f6345a5c92dcf138a4a0ba01430e9e49b2b57a20ed8d7fe9235a29643f
|
File details
Details for the file django_forms_frontend_validation-1.0.8-py3-none-any.whl.
File metadata
- Download URL: django_forms_frontend_validation-1.0.8-py3-none-any.whl
- Upload date:
- Size: 19.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bf1ee085f67dc36cf22489ea29c865b1c7ec520aab4c5b7d1ed03d883b74a69d
|
|
| MD5 |
973279f2160417d765714c36a5c93dd8
|
|
| BLAKE2b-256 |
26d469dcf3f41fbeab736e193498d4ed4fad0545da506777adfe13d43f374f44
|