I did some testing and answered my question however in doing so, also found something concerning. First, what I did:
1. Create New DocType ‘Example’
Kept it very simple. Two data fields, one named field_1
and the other named field_2
. This DocType was created in my custom app named example
.
2. Added Validate in Controller
In the controller for my Example DocType, I have the following:
import frappe
from frappe.model.document import Document
class Example(Document):
def validate( self ):
if self.field_1 is None:
frappe.throw( "Field 1 must be filled out" )
3. Added DocEvent To Hooks
In my hooks.py
file, I added the following:
doc_events = {
"Example": {
"validate": "example.example.doctype.example.override.validate"
}
}
And then I created the file /example/example/doctype/example/override.py
with the following content:
import frappe
from frappe.model.document import Document
def validate(doc, method=None):
if doc.field_2 is None:
frappe.throw( "Field 2 must be filled out" )
So both fields now are required to be filled out. Field 1 is checked in the actual controller itself and field 2 in the hook.
4. Restarted Frappe
After restarting Frappe, I tested by creating a new document for Example. Initially it looked like it was working as I was originally asking. Both validation functions are being run.
Concerning Part
The part that is concerning though is if I do the following:
After I hit save for either of the above, if I remove the value from the field that I had populated originally and put that value in the other field, hitting Save I would expect an error message saying the other field must be filled out. But no, the document is saving without an error. So it appears the validation is not running again.
So the question is, is this a bug in Frappe or is this expected behavior?