Override default validation

Hi guys,

Im trying to override the default validation at Expense Claim Type.

What im trying to achieve is to allow same company to be entered more than once.

This is default validation/

What i have done so far.

I create a .py with the same validation function as original expense claim type in my custom apps then at hook, i add validate under doc_event .

Im not sure if this a right method but im still not manage to override the validation yet hence im i need some help.

Thanks .

@ManasSolanki can you help?

Thanks for reply…im so glad if someone can help.

I think it is not possible right now.

This needs some more testing, but I was able to override validation (or any other function relevant at validation time) by introducing a new hook called “before_validate” to document.py.

## Two new lines added to document.py in run_before_save_methods
	def run_before_save_methods(self):
		"""Run standard methods before  `INSERT` or `UPDATE`. Standard Methods are:

		- `validate`, `before_save` for **Save**.
		- `validate`, `before_submit` for **Submit**.
		- `before_cancel` for **Cancel**
		- `before_update_after_submit` for **Update after Submit**

		Will also update title_field if set"""

		self.load_doc_before_save()
		self.reset_seen()

		if self.flags.ignore_validate:
			return

		if self._action=="save":
			self.run_method("before_validate") # new line
			self.run_method("validate")
			self.run_method("before_save")
		elif self._action=="submit":
			self.run_method("before_validate") # new line
			self.run_method("validate")
			self.run_method("before_submit")
		elif self._action=="cancel":
			self.run_method("before_cancel")
		elif self._action=="update_after_submit":
			self.run_method("before_update_after_submit")

		self.set_title_field()

Then, I added the hook to my app’s hooks.py:

doc_events = {
	"Relevant Doctype": {
	 	"before_validate":"path.to.override_validation"
	 }
}

Then, at the path listed above, I have the following function:

def override_validation(doc,method):
	RelevantDoctype.original_function = custom_function

I can get this posted as a pull request, but since it’s a relatively low-level function I thought it would be better to post it here first. Is there anything I am neglecting in this approach? It seems to work fine with no obvious performance implications.

6 Likes

can you please explain how can I override validate doc_event which is written in standard hooks.py file