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
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))
- Ensures that all fields marked as mandatory (
Field Length Validation
- Ensures that values do not exceed the defined field length.
- Applies to fields like
Data
,Int
, andCurrency
. - Example:
max_length = 140 if len(value) > max_length: frappe.throw(_("Value for {0} exceeds max length of {1}").format(fieldname, max_length))
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 of0
or1
.
- Ensures that field values match their defined data type (e.g.,
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))
- Ensures that fields marked as
Dependency Validation
- Validates fields with conditional dependencies (
depends_on
). - Example: A field is mandatory only if another field has a specific value.
- Validates fields with conditional dependencies (
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))
Link Field Validations
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))
- Ensures that linked documents (in
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.
- Ensures that
Cancelled Document Validation
- Prevents linking to documents with a
docstatus
of2
(Cancelled).
- Prevents linking to documents with a
Submit-Specific Validations
Workflow State Validation
- Ensures that the document is in the correct workflow state for submission.
Parent-Child Validation
- Ensures all child documents in tables meet validation rules (e.g., mandatory fields).
Constants Validation
- Prevents changes to fields marked as
set_only_once
after the document is submitted.
- Prevents changes to fields marked as
Docstatus Validation
- Validates the
docstatus
transition (e.g., Draft → Submitted, Submitted → Cancelled).
- Validates the
Value-Specific Validations
Date and Time Validation
- Ensures dates and times are valid and in the correct range.
- Example:
Due Date
cannot be earlier thanPosting Date
.
Field Comparisons
- Validates relationships between fields (e.g.,
To Date
must be afterFrom Date
).
- Validates relationships between fields (e.g.,
Numeric Range Validation
- Ensures numeric values fall within acceptable ranges.
Business Rule Validations
Custom Validations
- Business logic added through Python
validate
methods or Server Scripts. - Example: Ensuring stock levels are sufficient before creating a delivery note.
- Business logic added through Python
Budget Validations
- Enforces budget limits in financial transactions.
Inventory Validations
- Validates stock availability and batch/serial number requirements.
Data Integrity Validations
Child Table Validations
- Ensures that child table rows meet mandatory and field-specific validations.
Duplicate Row Validation
- Prevents duplicate rows in child tables (e.g., same item listed twice).
Consistency Across Fields
- Ensures consistency between related fields (e.g.,
Currency
in child table matches parent).
- Ensures consistency between related fields (e.g.,
System-Level Validations
Permissions Validation
- Ensures the user has the necessary permissions to perform the operation.
Script and Workflow Validations
- Validates against server-side scripts, client scripts, and workflow rules.
Versioning
- Ensures version control for tracking changes in the document.
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
Before Save (
before_save
)- Mandatory field checks.
- Field length and type checks.
Before Submit (
before_submit
)- Constants validation.
- Parent-child consistency checks.
After Submit (
on_submit
)- Business rule validations.
Before Cancel (
before_cancel
)- Checks for dependencies or linked documents.
After Cancel (
on_cancel
)- Updates related records (e.g., linked stock or accounting entries).
Before Delete (
before_delete
)- Validates data integrity (e.g., linked documents or references).