Skip to main content

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

Setting Up

  1. Include Required Components:
    • Include the JavaScript files in your project to enable client-side functionality.
    • Ensure form_utils.py is imported and accessible in your Django app.
  2. Define Settings in settings.py
    • Add formvalidator to 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', ...]
      IGNORE_VALIDATION = ['example-ignore-validation', ...]
      VALIDATE_ONLY_ON_SUBMIT = ['all']  # Options: "__all__", specific class names, or leave empty.
      
  3. Initial Forms:
    • Ensure the _InitializeForms method is called during page load to attach validation logic to forms dynamically. To your HTML template with the form, add this.
    <script src="{% static 'dist/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>
    
  4. Server-Side Context:
    • Use the FormsValidator class 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)
    

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_forms_frontend_validation-1.0.4.tar.gz (50.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file django_forms_frontend_validation-1.0.4.tar.gz.

File metadata

File hashes

Hashes for django_forms_frontend_validation-1.0.4.tar.gz
Algorithm Hash digest
SHA256 c2d988feefa8ac61213fe83deda3f896a8bdf28e5e37c5f1ed0373e3ab0688f4
MD5 ec30996db3d4caf62970ff756137b9a2
BLAKE2b-256 1a7eaeb4ffd6eb9b17edc4f070e28487dff0a4ffb40f13dcb1f526cfa43de9e3

See more details on using hashes here.

File details

Details for the file django_forms_frontend_validation-1.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for django_forms_frontend_validation-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 cc7e95aaf8e0ecd87bac50b6027751c0dda88f9e27ae239c9b23513a4769193c
MD5 35f3c4c5e07dae21f88586252c3255cc
BLAKE2b-256 d40c2e697207a0aa4421858a571e06eecea49b568027f992abb81386c7dbbde9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page