Trouble in validating

I have been developing an independent app named Container Management. For this purpose, i have created a doctype named Container having three fields container_id, size and mt_date. I have written the following code in container.py to validate the data in the server-side -

for row in frappe.get_all("Container", fields = ["container_id", "size"]):
    if len(row["container_id"]) != 11:
        frappe.throw('Container Id should be 11 characters long !!')
    if row["size"] != "20GP" or row["size"] != "40GP":
        frappe.throw('Size should be 20-GP or 40-GP') 

But when i reload in the app (before inputing any data) it throws the server error

Size should be 20-GP or 40-GP

Am i writing the code in wrong way?

Hard to guess, Share your full function (maybe you have not indented correctly)

2 Likes

I have written the code explicitly, not within a function. Here is the total code from the file -

# -*- coding: utf-8 -*-
# Copyright (c) 2015, Reza and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import frappe
from frappe.model.document import Document


class Container(Document):
    pass

for row in frappe.get_all("Container", fields = ["container_id", "size"]):
    if len(row["container_id"]) != 11:
        frappe.throw('Container Id should be 11 characters long !!')
    if row["size"] != "20GP" or row["size"] != "40GP":
        frappe.throw('Size should be 20-GP or 40-GP')

I have also guessed that the cause may be indentation, i have tried re-indentation, the result is same. :frowning:

indentation problem,try this code,

# -*- coding: utf-8 -*-
# Copyright (c) 2015, Reza and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import frappe
from frappe.model.document import Document


class Container(Document):
    def validate(self):
        for row in frappe.get_all("Container", fields = ["container_id", "size"]):
            if len(row["container_id"]) != 11:
                frappe.throw('Container Id should be 11 characters long !!')
            if row["size"] != "20GP" or row["size"] != "40GP":
                frappe.throw('Size should be 20-GP or 40-GP')

Sorry for late reply. I was away from keyboard. And thank you very much for your suggestion :slight_smile: . I have tried your suggestion, but still getting the same result. Is it have anything to do with cache ?

Another question, do i have to define methods inside Container class, everytime i am doing a query or adding validation ? Doesn’t code outside the class scope work?

Why are you using get_all()? I think you just want to validate the data in the current form. You code should be like this:

from __future__ import unicode_literals
import frappe
from frappe.model.document import Document

class Container(Document):
	def validate(self):
		if len(self.container_id) != 11:
			frappe.throw('Container Id should be 11 characters long !!')
		if self.size != "20GP" or self.size != "40GP":
			frappe.throw('Size should be 20-GP or 40-GP')

All the controller functions (validate, on_update, on_submit etc) should be inside the Class. Please check https://frappe.github.io/frappe/user/tutorial/controllers.html

2 Likes

Thank you very much. I didn’t know that the values from the form are automatically came with doctype.py :slight_smile:

Thanks again for the nice explanation. My validation has worked. And with your explanation, i can now write more robust queries and validations.