Validations in Document

In Frappe, several validations are performed during the lifecycle of a document to ensure data integrity, enforce business rules, and maintain consistent operations. These validations apply at various stages of document creation, saving, submitting, and updating. Here's an overview of the types of validations performed:


General Validations

  1. Mandatory Field Validation

    • Ensures that all fields marked as mandatory (reqd: 1) are filled.
    • Example:
      fieldname = "customer_name"
      if not self.get(fieldname):
          frappe.throw(_("Field {0} is mandatory").format(fieldname))
      
  2. Field Length Validation

    • Ensures that values do not exceed the defined field length.
    • Applies to fields like Data, Int, and Currency.
    • Example:
      max_length = 140
      if len(value) > max_length:
          frappe.throw(_("Value for {0} exceeds max length of {1}").format(fieldname, max_length))
      
  3. Field Type Validation

    • Ensures that field values match their defined data type (e.g., Int, Float, Currency).
    • Example: A Check field must have a value of 0 or 1.
  4. Unique Field Validation

    • Ensures that fields marked as unique do not have duplicate values.
    • Example:
      if frappe.db.exists(doctype, {"fieldname": value}):
          frappe.throw(_("Duplicate value found for {0}").format(fieldname))
      
  5. Dependency Validation

    • Validates fields with conditional dependencies (depends_on).
    • Example: A field is mandatory only if another field has a specific value.
  6. Field Options Validation

    • Ensures values in select fields are valid based on the defined options.
    • Example:
      if value not in ["Option 1", "Option 2"]:
          frappe.throw(_("Invalid value for {0}").format(fieldname))
      

  1. Linked Document Existence

    • Ensures that linked documents (in Link fields) exist.
    • Example:
      if not frappe.db.exists(linked_doctype, linked_name):
          frappe.throw(_("Invalid link in {0}").format(fieldname))
      
  2. Dynamic Link Validation

    • Ensures that Dynamic Link fields point to the correct document type.
    • Example: A Dynamic Link to "Customer" must actually reference a "Customer" document.
  3. Cancelled Document Validation

    • Prevents linking to documents with a docstatus of 2 (Cancelled).

Submit-Specific Validations

  1. Workflow State Validation

    • Ensures that the document is in the correct workflow state for submission.
  2. Parent-Child Validation

    • Ensures all child documents in tables meet validation rules (e.g., mandatory fields).
  3. Constants Validation

    • Prevents changes to fields marked as set_only_once after the document is submitted.
  4. Docstatus Validation

    • Validates the docstatus transition (e.g., Draft → Submitted, Submitted → Cancelled).

Value-Specific Validations

  1. Date and Time Validation

    • Ensures dates and times are valid and in the correct range.
    • Example:
      • Due Date cannot be earlier than Posting Date.
  2. Field Comparisons

    • Validates relationships between fields (e.g., To Date must be after From Date).
  3. Numeric Range Validation

    • Ensures numeric values fall within acceptable ranges.

Business Rule Validations

  1. Custom Validations

    • Business logic added through Python validate methods or Server Scripts.
    • Example: Ensuring stock levels are sufficient before creating a delivery note.
  2. Budget Validations

    • Enforces budget limits in financial transactions.
  3. Inventory Validations

    • Validates stock availability and batch/serial number requirements.

Data Integrity Validations

  1. Child Table Validations

    • Ensures that child table rows meet mandatory and field-specific validations.
  2. Duplicate Row Validation

    • Prevents duplicate rows in child tables (e.g., same item listed twice).
  3. Consistency Across Fields

    • Ensures consistency between related fields (e.g., Currency in child table matches parent).

System-Level Validations

  1. Permissions Validation

    • Ensures the user has the necessary permissions to perform the operation.
  2. Script and Workflow Validations

    • Validates against server-side scripts, client scripts, and workflow rules.
  3. Versioning

    • Ensures version control for tracking changes in the document.
  4. Attachment Validation

    • Ensures that uploaded files meet size, type, and other requirements.

Ignore Validations

The following validations can be bypassed using flags: - ignore_permissions: Skips user permission checks. - ignore_mandatory: Skips mandatory field checks. - ignore_validate: Skips validate hooks or methods. - ignore_links: Skips link field existence checks. - ignore_validate_update_after_submit: Allows changes to submitted documents.


Document Lifecycle Validations

  1. Before Save (before_save)

    • Mandatory field checks.
    • Field length and type checks.
  2. Before Submit (before_submit)

    • Constants validation.
    • Parent-child consistency checks.
  3. After Submit (on_submit)

    • Business rule validations.
  4. Before Cancel (before_cancel)

    • Checks for dependencies or linked documents.
  5. After Cancel (on_cancel)

    • Updates related records (e.g., linked stock or accounting entries).
  6. Before Delete (before_delete)

    • Validates data integrity (e.g., linked documents or references).

Discard
Save
Was this article helpful?

On this page